Setting up Basic Node.JS Server

Hey guys, I haven’t posted in a while, but I have been learning to mess around with node js now. Been watching and reading some tutorials online, and I wanted to post something basic.

This tutorial will allow you to create a website, and pretty much allow users to go onto different pages of your site.

 

var http = require('http');
var fs = require('fs');

function send404Response(response){
    response.writeHead(404, {"Context-Type": "text/plain"});
    response.write("Bad page");
    response.end();
}
function onRequest(request, response){

    if(request.method == 'GET' && request.url == '/'){
        response.writeHead(200, {"Context-Type": "text/html"});
        fs.createReadStream('./index.html').pipe(response);
    }
    else{
        return send404Response(response);
    }
}

http.createServer(onRequest).listen(8888);
console.log("Server is now ready");