Created
January 29, 2013 06:40
-
-
Save codemoran/4662306 to your computer and use it in GitHub Desktop.
Simple node.js HTTP webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('graphdat'); // require the graphdat module first, always! | |
var http = require("http"); // require the HTTP module | |
// | |
// Create the simple HTTP Server | |
// ### function createServer (requestCallback) | |
// #### @requestCallback {function} function is called on every request made. | |
// Create a Simple HTTP server. | |
// The request parameter will hold all request parameters | |
// The response parameter will hold all of the repsonse values sent to the client | |
// | |
http.createServer(function (request, response) { | |
// The 'end' event is called when the client has sent all of it's data | |
// and is waiting for response. | |
request.on("end", function () { | |
// Write the headers to the response. | |
// - first parameter is 200, is HTTP status code for success | |
// - second parameter holds header fields in object | |
// we are sending plain text, so Content-Type should be text/plain | |
// We are going to wrap it in a setTimeout, just so thing happen a little | |
// slower and we can see it popup on our graphs | |
setTimeout(function() { | |
response.writeHead(200, { | |
'Content-Type': 'text/plain' | |
}); | |
// Send data and end response. | |
response.end('Hello World, this page is being profiled with graphdat!'); | |
}, 150 + (Math.random() * 300) ); | |
}); | |
// Listen on the 8080 port. | |
}).listen(8080); | |
console.log('Server running at http://127.0.0.1:8080/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment