{"id":478,"date":"2024-04-25T02:25:31","date_gmt":"2024-04-25T02:25:31","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=478"},"modified":"2024-04-25T05:05:15","modified_gmt":"2024-04-25T05:05:15","slug":"nodejs-express-routing","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/04\/25\/nodejs-express-routing\/","title":{"rendered":"NODEJS \uc775\uc2a4\ud504\ub808\uc2a4 \ub77c\uc6b0\ud305 express routing"},"content":{"rendered":"\n<p>\uba3c\uc800 \ub2e4\uc74c\uacfc \uac19\uc774 public\ub514\ub809\ud1a0\ub9ac ,profile.html,.\/public\/index.html\ud30c\uc77c\uc744 \ub9cc\ub4ed\ub2c8\ub2e4.<br>profile.html\ud30c\uc77c\uacfc index.html\ud30c\uc77c\uc758 \ub0b4\uc6a9\uc740 \uc544\ub798\uc5d0 \uc788\uc2b5\ub2c8\ub2e4.<br>\uceec\ub7ec \uac15\uc870\ub41c \ubd80\ubd84\uc740 \uc0c8\ub85c \ucd94\uac00\ub41c \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>First, create the public directory, profile.html, and .\/public\/index.html files as follows.<br>The contents of the profile.html file and index.html file are below.<br>All parts highlighted in color are newly added codes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><img decoding=\"async\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/04\/D1-1.png\" alt=\"\"><\/td><td><br><img decoding=\"async\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/04\/D2-1.png\" alt=\"\" style=\"width: 290px;\"><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">public, profile.html, index.html<\/figcaption><\/figure>\n\n\n\n<p><br><strong>1.app.use(express.static(__dirname+&#8221;\/public&#8221;));<\/strong><br>&#8211; \ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c localhost:3000\uc2e4\ud589\uc2dc public\ub514\ub809\ud1a0\ub9ac \ub0b4\uc758 index.html\ud30c\uc77c\uc774 \uc2e4\ud589 \ub429\ub2c8\ub2e4.<br>&#8211; \uc81c\uac00 \uc0ac\uc6a9\ud558\ub294 PC\uc758 \uc808\ub300\uacbd\ub85c\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.E:\\nodejs\\lec\\public<br>&#8211; \ubcf4\ub77c\uc0c9\uc740 localhost:3000 \uc2e4\ud589\uc2dc &#8216;Express Hello World&#8217;\ub97c \ucd9c\ub825\ud558\uba70 \ub3d9\uc77c\ud55c \uae30\ub2a5\uc774\ub77c \ucf54\uba58\ud2b8 \ucc98\ub9ac \ud588\uc2b5\ub2c8\ub2e4.<\/p>\n\n\n\n<p>When you run localhost:3000 in your browser, the index.html file in the public directory is executed.<br>The absolute path on the PC I use is as follows: E:\\nodejs\\lec\\public<br>The purple color outputs &#8216;Express Hello World&#8217; when running localhost:3000, and has been commented out as it is the same function.<\/p>\n\n\n\n<p><strong>2.console.log(__dirname);<\/strong><br>&#8211; &#8216;__dirname&#8217; \ub514\ub809\ud1a0\ub9ac \ud658\uacbd\ubcc0\uc218\ub85c\uc368 console.log(__dirname);\uc744 \uc2e4\ud589\ud558\uba74 \ud604\uc7ac \ub514\ub809\ud1a0\ub9ac \uacbd\ub85c\ub97c \ucd9c\ub825\ud569\ub2c8\ub2e4.<br>When you run console.log(__dirname); with the &#8216;__dirname&#8217; directory environment variable, the current directory path is displayed.<\/p>\n\n\n\n<p><strong>3.app.get(&#8220;\/profile&#8221;,(req,res)=&gt;{}<\/strong><br>&#8211; &#8216;localhost:3000\/profile&#8217;\uc744 \uc2e4\ud589\ud588\uc744 \ub54c \ud604\uc7ac \ub514\ub809\ud1a0\ub9ac\uc5d0\uc11c profile.html\uc744 \uc2e4\ud589 \ud569\ub2c8\ub2e4. <br>When executing &#8216;localhost:3000\/profile&#8217;, profile.html is executed in the current directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/server_ex.js\nconst http = require('http');\nconst express = require('express');\nconst app = express();\nconst server = http.createServer(app);\n\n\/*  routing  *\/\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">app.use(express.static(__dirname+\"\/public\"));<\/mark>\n\n\/\/\ub514\ub809\ud1a0\ub9ac \ud658\uacbd\ubcc0\uc218 \n\/\/Directory environment variable\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">console.log(__dirname);<\/mark>\n\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#801fff\" class=\"has-inline-color\">\/*\napp.get('\/',(req, res) =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\n    res.end('Express Hello World!\\n');\n});\n*\/<\/mark>\n\n\/\/text\napp.get('\/about',(req, res) =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\n    res.end('Express About!\\n');\n});  \n\napp.get('\/random.text', function (req, res) {\n    res.send('Express random.text');\n});\n\n\/\/file\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-blue-color\">app.get(\"\/profile\",(req,res)=&gt;{\n\tres.sendFile(__dirname + \"\/profile.html\");\n});<\/mark>\n\n\n\/\/ Start the server\nconst PORT = process.env.PORT || 3000;\nserver.listen(PORT, () =&gt; {\n  console.log(`CommonJS(Express) Server is running on port ${PORT}`);\n});\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#944e05\" class=\"has-inline-color\">public\/index.html\n\n&lt;html>\n    &lt;head>    \n        &lt;title>index.html&lt;\/title>\n    &lt;\/head>\n    &lt;body>\n      index \uc778\ub371\uc2a4\n    &lt;\/body>\n&lt;\/html><\/mark><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#944e05\" class=\"has-inline-color\">profile.html\n\n&lt;html>\n    &lt;head>    \n        &lt;title>profile.html&lt;\/title>\n    &lt;\/head>\n    &lt;body>\n      profile \ud504\ub85c\ud544\n    &lt;\/body>\n&lt;\/html><\/mark><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uba3c\uc800 \ub2e4\uc74c\uacfc \uac19\uc774 public\ub514\ub809\ud1a0\ub9ac ,profile.html,.\/public\/index.html\ud30c\uc77c\uc744 \ub9cc\ub4ed\ub2c8\ub2e4.profile.html\ud30c\uc77c\uacfc index.html\ud30c\uc77c\uc758 \ub0b4\uc6a9\uc740 \uc544\ub798\uc5d0 \uc788\uc2b5\ub2c8\ub2e4.\uceec\ub7ec \uac15\uc870\ub41c \ubd80\ubd84\uc740 \uc0c8\ub85c \ucd94\uac00\ub41c \ucf54\ub4dc \uc785\ub2c8\ub2e4.First, create the public directory, profile.html, and .\/public\/index.html files as follows.The contents of the profile.html file and index.html file are below.All parts highlighted in color are newly added codes. public, profile.html, index.html 1.app.use(express.static(__dirname+&#8221;\/public&#8221;));&#8211; \ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c localhost:3000\uc2e4\ud589\uc2dc public\ub514\ub809\ud1a0\ub9ac \ub0b4\uc758 index.html\ud30c\uc77c\uc774 \uc2e4\ud589 \ub429\ub2c8\ub2e4.&#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-nodejs","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":12,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":497,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions\/497"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}