Skip to content

Instantly share code, notes, and snippets.

@alpersilistre
Last active December 29, 2015 10:09
Show Gist options
  • Save alpersilistre/1d25ada293dde0287b5e to your computer and use it in GitHub Desktop.
Save alpersilistre/1d25ada293dde0287b5e to your computer and use it in GitHub Desktop.
Node.js basic http web server file
var http = require('http');
var fs = require('fs');
const PORT = 8000;
function send404Response(response){
response.writeHead(404, {"Context-Type": "text/plain"});
response.write("Error 404: Page not found!");
response.end();
}
function handleRequest(request, response){
if(request.method == 'GET' && request.url == '/'){
response.writeHead(200, {"Context-Type": "text/html"});
fs.createReadStream("./index.html").pipe(response);
}
else{
send404Response(response);
}
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost: " + PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment