Skip to content

Instantly share code, notes, and snippets.

@jayco
Created October 28, 2012 10:01
Show Gist options
  • Save jayco/3968219 to your computer and use it in GitHub Desktop.
Save jayco/3968219 to your computer and use it in GitHub Desktop.
create new server instance
/* Create the new server:
Create server and pass it an anonymous function as the argument.
The reason for this is that the function will be
called whenever there is a new request to the webserver. The function is
directly attached to the new webservers (http_server) event listener for
the request event that is passed.
*/
http_server.createServer(
/* The requestListener function:
the two parameters are req (http.ServerRequest) and res (http.ServerResponse).
*/
function (req, res){
/* http.ServerResponse.writeHead:
This method sends a response status code
of 200, that all is well and good and provides the content type of
the response, in this case, text.
*/
res.writeHead(200, {'Content-Type' : 'text/plain'});
/* http.ServerResponse.end:
This method is used to confirm that the headers and response body
has been sent, indicating that the communication has finished.
*/
res.end('Hello World, from a little node! \n');
/* http.Server.listen:
This method that is appended to the createServer function is used to
listen for incoming connection on the port that is specified in the
first argument of the method call; 8214 in this example. The other
parameter is the host name, a callback function can also be added as
an additional parameter.
*/
}).listen(8124, "127.0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment