Created
March 11, 2013 00:14
-
-
Save reergymerej/5131118 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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