{"id":636,"date":"2022-05-20T13:41:46","date_gmt":"2022-05-20T13:41:46","guid":{"rendered":"http:\/\/mipython.magwebdesigns.net\/WP\/?p=636"},"modified":"2022-05-20T13:55:21","modified_gmt":"2022-05-20T13:55:21","slug":"node-js-tutorial-http-server-basic-html-templates","status":"publish","type":"post","link":"http:\/\/mipython.magwebdesigns.net\/WP\/2022\/05\/20\/node-js-tutorial-http-server-basic-html-templates\/","title":{"rendered":"Node JS Tutorial |  HTTP Server Basic HTML Templates"},"content":{"rendered":"\n<p><a href=\"https:\/\/replit.com\/@MANDREWS85\/NodeHTTPServer-Templates#index.js\">Node JS Tutorial |  HTTP Server Basic HTML Templates REPL Here<\/a><\/p>\n\n\n\n<p>Today we are adding a basic HTML template to our Node JS HTTP Server project.<\/p>\n\n\n\n<p>We are going to first require the file system module within Node.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"text codecolorer\">var fs = require('fs');<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Lets create a .html file named index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/><\/div><\/td><td><div class=\"text codecolorer\">fs.open('index.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('Saved!');<br \/>\n});<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Now append some HTML<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/><\/div><\/td><td><div class=\"text codecolorer\">fs.appendFile('index.html', '&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;HELLO WORLD&amp;lt;\/h1&amp;gt;&amp;lt;\/body&amp;gt;&amp;lt;\/html&amp;gt;', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING INDEX.HTML FILE');<br \/>\n});<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Then we open the index.html file and pass that as an argument into the server response to render.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/><\/div><\/td><td><div class=\"text codecolorer\">fs.readFile('.\/index.html', function (err, html) {<br \/>\n&nbsp; &nbsp; if (err) {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; throw err; <br \/>\n&nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; http.createServer(function(request, response) { &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; response.writeHeader(200, {&quot;Content-Type&quot;: &quot;text\/html&quot;}); &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; response.write(html); &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; response.end(); &nbsp;<br \/>\n&nbsp; &nbsp; }).listen(8000);<br \/>\n});<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/4720343\/loading-basic-html-in-node-js\">https:\/\/stackoverflow.com\/questions\/4720343\/loading-basic-html-in-node-js<\/a><\/p>\n\n\n\n<p>As you can see this is going to get a bit unwieldy.  In the next Tutorial lets look into an easier way to Template our project with Express and Pug.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node JS Tutorial | HTTP Server Basic HTML Templates REPL Here Today we are adding a basic HTML template to our Node JS HTTP Server project. We are going to first require the file system module within Node. Lets create a .html file named index.html Now append some HTML Then we open the index.html file and pass that as an argument into the server response to render. https:\/\/stackoverflow.com\/questions\/4720343\/loading-basic-html-in-node-js As you can see this is going to get a bit unwieldy. In the next Tutorial lets look into an easier way&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-636","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":5,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"predecessor-version":[{"id":650,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/636\/revisions\/650"}],"wp:attachment":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}