Last active
August 29, 2015 14:07
-
-
Save techslides/806491f32633a64a8524 to your computer and use it in GitHub Desktop.
Simple Node HTTP server
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
// 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