Node.js http server file content

Description

Node.js http server file content


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

var host = "127.0.0.1";
var port = 3000;//  ww  w .  ja va2  s.c  o m
var server = http.createServer(function(request, response){
  console.log("Received request: " + request.url + "\n");
  fs.readFile("./public" + request.url, function(error, data){
    if(error){
      response.writeHead(404, {"Content-type":"text/plain"});
      response.end("Page Not Found!");
    } else{
      response.writeHead(200, {"Content-type":"text/html"});
      response.end(data);
    }

  });

});

server.listen(port, host, function(){
  console.log("Listening " + host + ":" + port);
});



PreviousNext

Related