Node.js http create server using configuration file
var http = require("http"); var fs = require("fs"); var config = JSON.parse(fs.readFileSync("./files/server_config.json")); var host = config.host; var port = config.port; var server = http.createServer(function(request, response){ console.log("Received request: " + request.url); fs.readFile("./public" + request.url, function(error, data){ if (error) {/*from w w w .j av a 2 s. co m*/ response.writeHead(404, {"Content-type":"text/plain"}); response.end("Sorry the page was not found"); } else { response.writeHead(200, {"Content-type":"text/html"}); response.end(data); } }) }); server.listen(port, host, function(){ console.log("Listening " + host + ":" + port); }); fs.watchFile("./files/server_config.json", function(){ config = JSON.parse(fs.readFileSync("./files/server_config.json")); host = config.host; port = config.port; server.close(); server.listen(port, host, function(){ console.log("Now listening " + host + ":" + port); }); });