Skip to content

Instantly share code, notes, and snippets.

@Mostafa-Samir Mostafa-Samir/server.js Secret
Last active May 20, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.