{"id":10246,"date":"2013-05-10T23:19:31","date_gmt":"2013-05-10T17:49:31","guid":{"rendered":"https:\/\/2thenew.today\/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:\/\/2thenew.today\/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=\"https:\/\/2thenew.today\/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=\"https:\/\/2thenew.today\/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":5,"footnotes":""},"categories":[1],"tags":[1157,1156,1124,590],"class_list":["post-10246","post","type-post","status-publish","format-standard","hentry","category-technology","tag-http","tag-node","tag-node-js","tag-server"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Amit Kumar\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Create Basic HTTP Server with Node.js | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Create Basic HTTP Server with Node.js | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#article\",\"name\":\"Create Basic HTTP Server with Node.js | TO THE NEW Blog\",\"headline\":\"Create Basic HTTP Server with Node.js\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2013-05-10T23:19:31+05:30\",\"dateModified\":\"2016-12-19T15:31:02+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#webpage\"},\"articleSection\":\"Technology, http, node, node.js, server\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#listItem\",\"name\":\"Create Basic HTTP Server with Node.js\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#listItem\",\"position\":3,\"name\":\"Create Basic HTTP Server with Node.js\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/859356966acd3c3879759369c7ac72cfb81228287bfc78e99c3080bee9b6b9ba?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Amit Kumar\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/\",\"name\":\"Create Basic HTTP Server with Node.js | TO THE NEW Blog\",\"description\":\"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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/create-basic-http-server-with-node-js\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"datePublished\":\"2013-05-10T23:19:31+05:30\",\"dateModified\":\"2016-12-19T15:31:02+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Create Basic HTTP Server with Node.js | TO THE NEW Blog","description":"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","canonical_url":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#article","name":"Create Basic HTTP Server with Node.js | TO THE NEW Blog","headline":"Create Basic HTTP Server with Node.js","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/amit-kumar\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2013-05-10T23:19:31+05:30","dateModified":"2016-12-19T15:31:02+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#webpage"},"articleSection":"Technology, http, node, node.js, server"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.today\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#listItem","name":"Create Basic HTTP Server with Node.js"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#listItem","position":3,"name":"Create Basic HTTP Server with Node.js","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/amit-kumar\/#author","url":"https:\/\/2thenew.today\/blog\/author\/amit-kumar\/","name":"Amit Kumar","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/859356966acd3c3879759369c7ac72cfb81228287bfc78e99c3080bee9b6b9ba?s=96&d=mm&r=g","width":96,"height":96,"caption":"Amit Kumar"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#webpage","url":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/","name":"Create Basic HTTP Server with Node.js | TO THE NEW Blog","description":"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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/amit-kumar\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/amit-kumar\/#author"},"datePublished":"2013-05-10T23:19:31+05:30","dateModified":"2016-12-19T15:31:02+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Create Basic HTTP Server with Node.js | TO THE NEW Blog","og:description":"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","og:url":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Create Basic HTTP Server with Node.js | TO THE NEW Blog","twitter:description":"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","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"10246","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:08:02","updated":"2024-02-29 10:52:24","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCreate Basic HTTP Server with Node.js\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Technology","link":"https:\/\/2thenew.today\/blog\/category\/technology\/"},{"label":"Create Basic HTTP Server with Node.js","link":"https:\/\/2thenew.today\/blog\/create-basic-http-server-with-node-js\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/10246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=10246"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/10246\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=10246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=10246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=10246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}