Skip to content

Instantly share code, notes, and snippets.

@Mostafa-Samir
Last active May 20, 2017 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mostafa-Samir/8d88882e223a43bbbdef to your computer and use it in GitHub Desktop.
Save Mostafa-Samir/8d88882e223a43bbbdef to your computer and use it in GitHub Desktop.
A sample HTTP server application for tutorial here: http://mostafa-samir.github.io/async-recursive-patterns-pt2/
var http = require('http');
var server = http.createServer(function(request, response) {
var randomNetworkDelayFactor = Math.floor(Math.random() * 99) + 1;
// simulates a random network delay up to one second
setTimeout(function() {
var request_path = request.url;
response.writeHead(200, {"Content-Type": "application/json"});
if(request_path === '/meta/moduleA') {
var dataA = JSON.stringify({
"hasDependency": true,
"dependency": "moduleB"
});
response.end(dataA);
}
else if(request_path === '/meta/moduleB') {
var dataB = JSON.stringify({
"hasDependency": true,
"dependency": "moduleC"
});
response.end(dataB);
}
else if(request_path === '/meta/moduleC') {
var dataC = JSON.stringify({
"hasDependency": false,
});
response.end(dataC);
}
else {
response.end(JSON.stringify({
"err": "NOT FOUND"
}));
}
}, randomNetworkDelayFactor * 10);
});
server.listen(8080);
console.log("The server is running on http://localhost:8080");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment