{"id":1065,"date":"2015-08-24T15:31:45","date_gmt":"2015-08-24T14:31:45","guid":{"rendered":"http:\/\/blogs.silicontechnix.com\/?p=1065"},"modified":"2015-08-24T15:32:15","modified_gmt":"2015-08-24T14:32:15","slug":"simple-web-server-in-15-lines-of-code","status":"publish","type":"post","link":"https:\/\/blogs.silicontechnix.com\/?p=1065","title":{"rendered":"Simple Web server in 15 lines of code"},"content":{"rendered":"<p>I was trying to build a webserver for just displaying a simple message but I don&#8217;t want to use Apache\/IIS\/nginx etc.<br \/>\nSo I tried nodejs and literally it is so easy to write a webserver in just 15 lines of code.<\/p>\n<p>Webserver:<\/p>\n<ul>\n<li>which can show static index.html page<\/li>\n<li>which can log remote client IPs<\/li>\n<li>which can use port from commandline to listen.<\/li>\n<li>Portable, can run same code on Windows\/Linux<\/li>\n<\/ul>\n<p><!--more-->So less talk more work, Hero you go<\/p>\n<pre class=\"brush:jscript\">var http = require('http');\r\nvar fs = require('fs');\r\nvar index = fs.readFileSync('index.html');\r\nvar port = process.argv[2];\r\nif (port===undefined)\r\n{\r\n\tconsole.log(\"No Listening Port provided, Server will start listening on port 80\");\r\n\tport=80;\r\n}\r\nconsole.log(\"Server Start Listening on Port: \" + port);\r\nhttp.createServer(function (req, res) {\r\n  res.writeHead(200, {'Content-Type': 'text\/html'});\r\n  res.end(index);\r\n  console.log(\"Remote IP: \" + req.connection.remoteAddress);\r\n}).listen(port);\r\n<\/pre>\n<p>That&#8217;s it, I am adding <a href=\"https:\/\/blogs.silicontechnix.com\/attachments\/SimpleWebServer.zip\" target=\"_blank\">binary<\/a> 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.<\/p>\n<p>Click <a href=\"https:\/\/blogs.silicontechnix.com\/attachments\/SimpleWebServer.zip\" target=\"_blank\">here <\/a>to download binary<br \/>\nEnjoy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to build a webserver for just displaying a simple message but I don&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"no","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[12,95],"tags":[489,485,488,483,486,487,484],"class_list":["post-1065","post","type-post","status-publish","format-standard","hentry","category-programming","category-system-administration","tag-coding","tag-html","tag-listen","tag-nodejs","tag-open-port","tag-tcp","tag-webserver"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p12j6H-hb","_links":{"self":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/1065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1065"}],"version-history":[{"count":9,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/1065\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/1065\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}