Skip to content

Instantly share code, notes, and snippets.

@techslides
Last active August 29, 2015 14:07
Show Gist options
  • Save techslides/806491f32633a64a8524 to your computer and use it in GitHub Desktop.
Save techslides/806491f32633a64a8524 to your computer and use it in GitHub Desktop.
Simple Node HTTP server
// Load the http module to create an http server.
var http = require('http');
// Create a function to handle every HTTP request
function handler(req, res){
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end("<html><body><h1>Hello</h1></body></html>");
};
// Create a server that invokes the `handler` function upon receiving a request
http.createServer(handler).listen(8000, function(err){
if(err){
console.log('Error starting http server');
} else {
console.log("Server running at http://127.0.0.1:8000/ or http://localhost:8000/");
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment