{"id":7422,"date":"2019-09-27T21:42:50","date_gmt":"2019-09-27T21:42:50","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7422"},"modified":"2019-09-27T21:42:50","modified_gmt":"2019-09-27T21:42:50","slug":"simple-node-based-http-server","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/simple-node-based-http-server\/","title":{"rendered":"Simple node based HTTP server"},"content":{"rendered":"<p>In a previous post we used webpack to run an HTTP server, in this post we&#8217;re going to create a bare bones HTTP server using the <em>http<\/em> package.<\/p>\n<p>In your chosen folder run the usual commands<\/p>\n<ul>\n<li>yarn init y<\/li>\n<li>tsc &#8211;init<\/li>\n<li>Add a folder named public off of the folder you source will be in (this will be where we add static html files)<\/li>\n<\/ul>\n<p>Now lets&#8217; add the required packages<\/p>\n<ul>\n<li>yarn add node-static @types\/node-static<\/li>\n<\/ul>\n<p>Add the following to the package.json<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n  &quot;start&quot;: &quot;node server.js&quot;,\r\n  &quot;build&quot;: &quot;tsc&quot;\r\n}\r\n<\/pre>\n<p>In the public folder add index.html with the following <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n  &lt;head&gt;&lt;\/head&gt;\r\n  &lt;body&gt;\r\n  Hello World\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now let&#8217;s add the code to start up our server (mine&#8217;s in the file server.ts)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport ns from &quot;node-static&quot;;\r\nimport http from &quot;http&quot;\r\n\r\nconst file = new ns.Server(&quot;.\/public&quot;);\r\n\r\nhttp.createServer((request, response) =&gt; {\r\n    request.addListener(&quot;end&quot;, () =&gt; {\r\n        file.serve(request, response);\r\n    })\r\n    .resume()\r\n})\r\n.listen(4000);\r\n<\/pre>\n<p>Simple run <em>yarn build<\/em> then <em>yarn start<\/em> and the server will start. Navigating your preferred browser to http:\/\/localhost:4000\/ will then display the HTML file, i.e. Hello World text.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post we used webpack to run an HTTP server, in this post we&#8217;re going to create a bare bones HTTP server using the http package. In your chosen folder run the usual commands yarn init y tsc &#8211;init Add a folder named public off of the folder you source will be in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[42,45,46],"tags":[],"class_list":["post-7422","post","type-post","status-publish","format-standard","hentry","category-html","category-javascript","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7422"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7422\/revisions"}],"predecessor-version":[{"id":7462,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7422\/revisions\/7462"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}