Last active
December 29, 2015 10:09
-
-
Save alpersilistre/1d25ada293dde0287b5e to your computer and use it in GitHub Desktop.
Node.js basic http web server file
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
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