Skip to content

Instantly share code, notes, and snippets.

@reergymerej
Created March 11, 2013 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reergymerej/5131118 to your computer and use it in GitHub Desktop.
Save reergymerej/5131118 to your computer and use it in GitHub Desktop.
// import the http module
var http = require('http');
// create a server
var server = http.createServer();
// define a handler for the 'request' event, triggered
// when a client makes a request of the server -
// request and response objects will be passed to the handler
server.on('request', function(req, res){
// define a handler for the 'end' event, triggered once
// the request is finished
req.on('end', function(){
// output request info to the console, so we know
// something's happening
console.log(new Date() + ' - ' + req.url);
// write a basic response header
res.writeHead(200, {
'content-type': 'text/plain'
});
// write the body of the response
res.write('Hello, world.');
// end the response
res.end();
});
});
// tell the server to listen for requests on port 3000
server.listen(3000);
// output to the console that the server is ready
// to handle requests
console.log('listening for requests on port 3000...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment