{"id":655,"date":"2022-05-20T17:13:18","date_gmt":"2022-05-20T17:13:18","guid":{"rendered":"http:\/\/mipython.magwebdesigns.net\/WP\/?p=655"},"modified":"2022-05-20T17:14:57","modified_gmt":"2022-05-20T17:14:57","slug":"node-js-and-express-routing-tutorial","status":"publish","type":"post","link":"http:\/\/mipython.magwebdesigns.net\/WP\/2022\/05\/20\/node-js-and-express-routing-tutorial\/","title":{"rendered":"Node JS and Express Routing Tutorial"},"content":{"rendered":"\n<p><a href=\"https:\/\/replit.com\/@MANDREWS85\/ExpressRouting#index.js\">Node JS and Express Routing Tutorial<\/a><\/p>\n\n\n\n<p>Lets set up our Express app with some different routes now. Lets make a basic company or personal web site structure.  With an about,contact,links and portfolio pages.<\/p>\n\n\n\n<p>First lets make the files with Node file system module.<\/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 \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/><\/div><\/td><td><div class=\"text codecolorer\">var fs = require('fs');<br \/>\n<br \/>\n\/\/ &nbsp; MAKE NEW FILE NAMED INDEX.HTML<br \/>\nfs.open('index.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('SAVED NEW INDEX.HTML FILE');<br \/>\n}); <br \/>\n\/\/ MAKE ABOUT PAGE<br \/>\nfs.open('about.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('SAVED NEW ABOUT.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ MAKE PORTFOLIO PAGE<br \/>\nfs.open('portfolio.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('SAVED NEW PORTFOLIO.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ MAKE CONTACT PAGE<br \/>\nfs.open('contact.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('SAVED NEW CONTACT.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ MAKE LINKS PAGE<br \/>\nfs.open('links.html', 'w', function (err, file) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('SAVED NEW LINKS.HTML FILE');<br \/>\n});<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Then lets append some basic HTML into the files.<\/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 \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/><\/div><\/td><td><div class=\"text codecolorer\">\/\/ APPEND FILE INDEX.HTML<br \/>\nfs.appendFile('index.html', 'INDEX new text', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING INDEX.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ APPEND FILE ABOUT.HTML<br \/>\nfs.appendFile('about.html', 'ABOUT new text', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING ABOUT.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ APPEND FILE PORTFOLIO.HTML<br \/>\nfs.appendFile('portfolio.html', 'PORTFOLIO new text', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING PORTFOLIO.HTML FILE');<br \/>\n});<br \/>\n<br \/>\n\/\/ APPEND FILE CONTACT.HTML<br \/>\nfs.appendFile('contact.html', 'CONTACT new text', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING CONTACT.HTML FILE');<br \/>\n}); <br \/>\n<br \/>\n\/\/ APPEND FILE LINKS.HTML<br \/>\nfs.appendFile('links.html', 'LINKS new text', function (err) {<br \/>\n&nbsp; if (err) throw err;<br \/>\n&nbsp; console.log('APPENDING LINKS.HTML FILE');<br \/>\n});<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Now lets define the routes we want.<\/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 \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/><\/div><\/td><td><div class=\"text codecolorer\">var express = require(&quot;express&quot;);<br \/>\nvar app = express();<br \/>\nvar path = require(&quot;path&quot;);<br \/>\n<br \/>\n\/\/ SERVE ROOT<br \/>\napp.get('\/',function(req,res){<br \/>\n&nbsp; res.sendFile(path.join(__dirname+'\/index.html'));<br \/>\n});<br \/>\n\/\/SERVE ABOUT<br \/>\napp.get('\/about',function(req,res){<br \/>\n&nbsp; res.sendFile(path.join(__dirname+'\/about.html'));<br \/>\n});<br \/>\n\/\/SERVE PORTFOLIO<br \/>\napp.get('\/portfolio',function(req,res){<br \/>\n&nbsp; res.sendFile(path.join(__dirname+'\/portfolio.html'));<br \/>\n});<br \/>\n\/\/SERVE CONTACT<br \/>\napp.get('\/contact',function(req,res){<br \/>\n&nbsp; res.sendFile(path.join(__dirname+'\/contact.html'));<br \/>\n});<br \/>\n\/\/SERVE LINKS<br \/>\napp.get('\/links',function(req,res){<br \/>\n&nbsp; res.sendFile(path.join(__dirname+'\/links.html'));<br \/>\n});<br \/>\n<br \/>\napp.listen(3000);<br \/>\nconsole.log(&quot;Running at Port 3000&quot;);<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Now we have a web site up with multiple routes.<\/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\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node JS and Express Routing Tutorial Lets set up our Express app with some different routes now. Lets make a basic company or personal web site structure. With an about,contact,links and portfolio pages. First lets make the files with Node file system module. Then lets append some basic HTML into the files. Now lets define the routes we want. Now we have a web site up with multiple routes.<\/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-655","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/655","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=655"}],"version-history":[{"count":2,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":657,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/655\/revisions\/657"}],"wp:attachment":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}