{"id":10246,"date":"2013-05-10T23:19:31","date_gmt":"2013-05-10T17:49:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10246"},"modified":"2016-12-19T15:31:02","modified_gmt":"2016-12-19T10:01:02","slug":"create-basic-http-server-with-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-basic-http-server-with-node-js\/","title":{"rendered":"Create Basic HTTP Server with Node.js"},"content":{"rendered":"<p>Like other languages(Java, php), we do not need to set up any <strong>Apache<\/strong> <strong>HTTP<\/strong> <strong>server<\/strong>. With <strong>Node<\/strong>, things are a bit different, with <strong>Node.js<\/strong>, we not only implement our application, we also implement the whole <strong>HTTP<\/strong> <strong>server<\/strong>. In fact, our web application and its web <strong>server<\/strong> are basically the same. Lets create a basic <strong>Node<\/strong> <strong>HTTP<\/strong> <strong>server<\/strong>. First let&#8217;s create a main file called <strong>server.js<\/strong> in the root directory which we use to start our application, and a module file where our <strong>HTTP<\/strong> <strong>server<\/strong> code lives, and fill it with the code given below:<\/p>\n<p>[js]<br \/>\nvar http = require(&#8220;http&#8221;);<br \/>\nvar requestHandler = function(request, response) {<br \/>\n  console.log(&#8220;Request received.&#8221;);<br \/>\n  response.writeHead(200, {&#8220;Content-Type&#8221;: &#8220;text\/plain&#8221;});<br \/>\n  response.write(&#8220;Hello World&#8221;);<br \/>\n  response.end();<br \/>\n};<br \/>\nvar server = http.createServer(requestHandler);<br \/>\nserver.listen(9999);<br \/>\nconsole.log(&#8220;Server has started.&#8221;);<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p>Now run the code. To run the <strong>server.js<\/strong> script with <strong>Node.js<\/strong> type below command:<\/p>\n<p>[bash]<br \/>\nnode server.js<br \/>\n[\/bash]<\/p>\n<p style=\"padding:10px\">\n<p>Now open your favorite browser and hit it at http:\/\/localhost:9999\/. This should display a web page that says <strong>&#8220;Hello World!&#8221;<\/strong>. Well, now lets analyze what&#8217;s actually going on here.<\/p>\n<p style=\"padding:10px\">\n<p>1. The first line <strong>requires<\/strong> the <strong>http<\/strong> module that ships with <strong>Node.js<\/strong> and makes it accessible through the variable <strong>http<\/strong>.<br \/>\n2. We definded a <strong>requestHandler<\/strong> funciton to handle all the requests.<br \/>\n3. We called one of the functions that <strong>http<\/strong> module offers: <strong>createServer<\/strong>. This function returns an object, which we store in server.<br \/>\n4. This(<strong>server<\/strong>) object has a method named <strong>listen<\/strong>, and takes a numeric value which indicates the port number our <strong>HTTP<\/strong> <strong>server<\/strong> is going to <strong>listen<\/strong>.<\/p>\n<p style=\"padding:10px\">\n<p style=\"padding:10px\">\n<p>Now lets analyze the, how <strong>requestHandler<\/strong> handling the requests. Whenever a request received, <strong>requestHandler<\/strong> function gets triggered and two parameters are passed into it, <strong>request<\/strong> and <strong>response<\/strong>.<\/p>\n<p style=\"padding:10px\">\n<p>1. <strong>response.writeHead()<\/strong> send an HTTP status and content-type in the <strong>HTTP<\/strong> <strong>response<\/strong> header.<br \/>\n2. <strong>response.write()<\/strong> send the text <strong>\u201cHello World\u201d<\/strong> in the <strong>HTTP<\/strong> <strong>response<\/strong> body.<br \/>\n3. <strong>response.end()<\/strong> finish <strong>response<\/strong>.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like other languages(Java, php), we do not need to set up any Apache HTTP server. With Node, things are a bit different, with Node.js, we not only implement our application, we also implement the whole HTTP server. In fact, our web application and its web server are basically the same. Lets create a basic Node [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1157,1156,1124,590],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10246"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10246"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10246\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}