Skip to content

Instantly share code, notes, and snippets.

@butaji
Last active February 7, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save butaji/78fd00624dd3e2981683 to your computer and use it in GitHub Desktop.
Save butaji/78fd00624dd3e2981683 to your computer and use it in GitHub Desktop.
Code sample for artice Practical Microservices: Integration Tests and Stub Services https://medium.com/@butaji/practical-microservices-integration-tests-and-stub-services-80749ce01050#.zdyycghvf
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/setup', (req, res) => {
const body = req.body;
console.log(body);
if (body.method == 'get') {
app.get(body.url, (req_m, res_m) => {
res_m.send(body.response);
});
app.listen(body.port, () => {
console.log('Listening ' + body.port);
res.send('Done!');
});
}
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/orders', (req, res) => {
checkAccout(req.body.userId, res);
});
app.listen(8080, () => {
console.log('Example app listening on port 8080!');
});
function checkAccout(userId, res) {
request('http://localhost:3001/account/' + userId + '/balance', function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body > 0) {
res.send('order placed');
} else {
res.send('insufficient funds');
}
} else {
res.send('error ' + error);
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment