Skip to content

Instantly share code, notes, and snippets.

@kjenney
Created October 28, 2018 02:41
Show Gist options
  • Save kjenney/3f53b6a51ba5421e46d287f9a06c86c8 to your computer and use it in GitHub Desktop.
Save kjenney/3f53b6a51ba5421e46d287f9a06c86c8 to your computer and use it in GitHub Desktop.
Two
var nock = require('nock')
var http = require('http');
var request = require('request-promise');
nock('http://greetings_api:3000')
.post('/getGreeting')
.matchHeader('content-type', 'application/json')
.matchHeader('language', 'es')
.matchHeader('host', 'greetings_api:3000')
.matchHeader('content-length', 0)
.reply(200, 'Hola');
var greetingOptions = {
uri: 'http://greetings_api:3000/getGreeting',
hostname: 'greetings_api',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'language': 'es'
}
};
var greeting = {
getGreeting: function() {
return request(greetingOptions);
}
}
function myGreeting() {
return greeting.getGreeting();
};
nock('http://users_api:3000')
.post('/getUser')
.matchHeader('content-type', 'application/json')
.matchHeader('id', 1)
.matchHeader('host', 'users_api:3000')
.matchHeader('content-length', 0)
.reply(200, 'Jose Smith');
var userOptions = {
uri: 'http://users_api:3000/getUser',
hostname: 'users_api',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'id': 1
}
};
var user = {
getUser: function() {
return request(userOptions);
}
}
function myUser() {
return user.getUser();
}
async function getWelcome(req, res) {
try {
const [user, greeting] = await Promise.all([
myUser(),
myGreeting()
]);
res.send(greeting + ' ' + user);
}
catch(e) {
res.status(404).send();
}
}
module.exports = { getWelcome };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment