Simple Web server in 15 lines of code

I was trying to build a webserver for just displaying a simple message but I don’t want to use Apache/IIS/nginx etc.
So I tried nodejs and literally it is so easy to write a webserver in just 15 lines of code.

Webserver:

  • which can show static index.html page
  • which can log remote client IPs
  • which can use port from commandline to listen.
  • Portable, can run same code on Windows/Linux

So less talk more work, Hero you go

var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');
var port = process.argv[2];
if (port===undefined)
{
	console.log("No Listening Port provided, Server will start listening on port 80");
	port=80;
}
console.log("Server Start Listening on Port: " + port);
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
  console.log("Remote IP: " + req.connection.remoteAddress);
}).listen(port);

That’s it, I am adding binary in this post, so if somebody just needs to check open ports, without installing nodejs or playing with the code etc., you can provide Listening Port from command line to troubleshoot or display simple message from your computers.

Click here to download binary
Enjoy