Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require("http");
- var url = require("url");
- var fakeDatabase = [{"first":"joeNetwork1","last":"Smith1","email":"joeNetworksmith1@gmail.com","id":0.5150849785432448},{"first":"joeNetwork2","last":"Smith2","email":"joeNetworksmith2@gmail.com","id":0.3907262226520378},{"first":"joeNetwork3","last":"Smith3","email":"joeNetworksmith3@gmail.com","id":0.0789635039407276}];
- function onRequest(req, res) {
- var parsed = url.parse(req.url,true);
- var pathname = parsed.pathname;
- console.log("Request for " + pathname + " received.");
- if (pathname == "/contacts") {
- res.writeHead(200, {"Content-Type": "application/javascript"});
- var responseBody = JSON.stringify(fakeDatabase);
- if (parsed.query.callback) //JSONP
- res.write(parsed.query.callback + "(");
- res.write(responseBody); // JSON OR JSONP
- if (parsed.query.callback)
- res.write(");"); //JSONP
- }
- else {
- res.writeHead(200, {"Content-Type": "text/html"});
- res.write("<h4>Hello World</h4>");
- for (var key in parsed.query)
- res.write(key + " => "
- + parsed.query[key] + "<br/>");
- }
- res.end();
- }
- http.createServer(onRequest).listen(8888); //diff for mgd env
- console.log("It ... is ... alive!");
Add Comment
Please, Sign In to add comment