{"id":462,"date":"2024-04-23T01:06:58","date_gmt":"2024-04-23T01:06:58","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=462"},"modified":"2024-04-23T02:08:12","modified_gmt":"2024-04-23T02:08:12","slug":"nodejs-basic-httpserver-npm-expressrouting","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/04\/23\/nodejs-basic-httpserver-npm-expressrouting\/","title":{"rendered":"NODEJS\uae30\ubcf8 http server ,NODEJS Basic http server(npm express,routing)"},"content":{"rendered":"\n<p>express \ubaa8\ub4c8\uc740 \ucee4\ub9e8\ub4dc\ub77c\uc778\uc5d0\uc11c npm \uba85\ub839\uc5b4\ub85c \uc124\uce58 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>\ube0c\ub77c\uc6b0\uc800 \uc8fc\uc18c\ub97c \ud1b5\ud574\uc11c \uc6f9\uc0ac\uc774\ud2b8\uc5d0 \uc811\uc18d \ud560 \ub54c \ub3c4\uba54\uc778 \uc8fc\uc18c \uc5d4\ub4dc \ud3ec\uc778\ud2b8\uc5d0 \ub530\ub77c \uc6f9\uc11c\ubc84\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \ub2e4\ub978 \ub0b4\uc6a9\uc744 \ubcf4\uc5ec \uc904 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>\uc608\ub97c \ub4e4\uc5b4\uc11c  http:\/\/freelifemakers.org\/wordpress \ub77c\uace0 \ud55c\ub2e4\uba74 \uc774 &#8216;\/wordpress&#8217; \ubd80\ubd84\uc744 \uc5d4\ub4dc\ud3ec\uc778\ud2b8\ub77c\uace0 \ud569\ub2c8\ub2e4. <br>\ub2e4\ub978 \ub9d0\ub85c \ub77c\uc6b0\ud2b8 \ub610\ub294 \ub8e8\ud2b8\ub77c\uace0 \uc815\uc758 \ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.   <br>\uc774\ub807\uac8c \uc5d4\ub4dc\ud3ec\uc778\ud2b8\uc5d0 \ub530\ub77c \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0 \ub2e4\ub978 \uc751\ub2f5\uc744 \ud558\ub294 \uae30\ub2a5\uc744 \ub77c\uc6b0\ud305\uc774\ub77c\uace0 \ud569\ub2c8\ub2e4.<br>express\ub294 \uc774\ub807\uac8c \ub77c\uc6b0\ud305 \uae30\ub2a5\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uac8c \ud574\uc8fc\ub294 \ubaa8\ub4c8\uc774\ub77c\uace0 \uc0dd\uac01\ud558\uc2dc\uba74 \ub429\ub2c8\ub2e4.<\/p>\n\n\n\n<p>The express module can be installed with the npm command from the command line.<br>When you access a website through a browser address, depending on the domain address endpoint, the web server can show the client different content.<br>For example, if it&#8217;s http:\/\/freelifemakers.org\/wordpress , this &#8216;\/wordpress&#8217; part is called an endpoint.<br>In other words, it can be defined as a route.<br>The ability to respond differently to clients depending on the endpoint is called routing.<br>You can think of express as a module that allows you to use routing capabilities like this.<br><br><strong>1.\uc124\uce58\ubc29\ubc95(Installation) <\/strong><br>-js\ud30c\uc77c \ub514\ub809\ud1a0\ub9ac\ub85c \uc774\ub3d9\ud574\uc11c \ucee4\uba58\ub4dc\ub77c\uc778\uc5d0\uc11c \ub2e4\uc74c\uacfc \uac19\uc774 \uc785\ub825\ud569\ub2c8\ub2e4.<br>-Go to the js file directory and enter the following on the command line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#npm install express<\/code><\/pre>\n\n\n\n<p><strong>2.\ub77c\uc6b0\ud305(routing)<\/strong><br>-\ub178\ub780\uc0c9,\ub179\uc0c9,\ud30c\ub780\uc0c9 \ubd80\ubd84\uc740 \ubaa8\ub450 express\ub97c \uc704\ud55c \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>-\ub178\ub780\uc0c9\uc740 \ubaa8\ub4c8 \ud3ec\ud568 \uc2dc\ud0a4\ub294 \ubd80\ubd84\uc774\uace0,\ub179\uc0c9\uacfc \ud30c\ub791 \uc0c9\uc740 get\ubc29\uc2dd(\ube0c\ub77c\uc6b0\uc800 \uc8fc\uc18c\ud45c\uc2dc\uc904)\uc744 \ud1b5\ud55c \uc811\uadfc\uc5d0 \ub300\ud55c \ubd80\ubd84\uc785\ub2c8\ub2e4.<br>-\ub178\ub780\uc0c9 \ud654\uc0b4\ud45c\ud568\uc218\uc640  \ud558\ub298\uc0c9 \uae30\uc874 \uc790\ubc14\uc2a4\ud06c\ub9bd\ud2b8 \ud568\uc218\uac00 \ubaa8\ub450 \uc0ac\uc6a9 \uac00\ub2a5 \ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<p>-The yellow, green, and blue parts are all codes for express.<br>-The yellow color is the part that includes the module, and the green and blue colors are the part for access through the get method (browser address bar).<br>-Both yellow arrow functions and light blue existing JavaScript type functions are available.<\/p>\n\n\n\n<p><br><strong>1)app.get(&#8216;\/&#8217;, (req, res) =&gt; {}<\/strong><br>http:\/\/freelifemakers.org \ub610\ub294 http:\/\/localhost \ucc98\ub7fc \uc811\uc18d\ud588\uc744 \ub54c \ubcf4\uc5ec\uc904 \ub0b4\uc6a9\uc744 \uc124\uc815\ud569\ub2c8\ub2e4.<br>Set the content to be displayed when accessed, such as http:\/\/freelifemakers.org or http:\/\/localhost.<br><br><strong>2)app.get(&#8216;\/about&#8217;,(req, res) =&gt; {}<\/strong><br>http:\/\/freelifemakers.org\/about \ub610\ub294 http:\/\/localhost\/about \ucc98\ub7fc \uc811\uc18d\ud588\uc744 \ub54c \ubcf4\uc5ec\uc904 \ub0b4\uc6a9\uc744 \uc124\uc815\ud569\ub2c8\ub2e4.<br>Set the content to be displayed when accessed, such as http:\/\/freelifemakers.org\/about or http:\/\/localhost\/about.<br><br><strong>3)app.get(&#8216;\/random.text&#8217;, function (req, res) {}<\/strong><br>http:\/\/freelifemakers.org\/random.text \ub610\ub294 http:\/\/localhost\/random.text \ucc98\ub7fc \uc811\uc18d\ud588\uc744 \ub54c \ubcf4\uc5ec\uc904 \ub0b4\uc6a9\uc744 \uc124\uc815\ud569\ub2c8\ub2e4.<br>Set the content to be displayed when accessed, such as http:\/\/freelifemakers.org\/random.text or http:\/\/localhost\/random.text.<\/p>\n\n\n\n<p><strong>3.\uc804\uccb4\ucf54\ub4dc(full code)<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/server_ex.js\nconst http = require('http');\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#d6b31a\" class=\"has-inline-color\">const express = require('express');\nconst app = express();\nconst server = http.createServer(app);<\/mark>\n\n\/\/routing\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">app.get('\/',(req, res)<\/mark> =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\n    res.end('Express Hello World!\\n');\n});\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">app.get('\/about',(req, res)<\/mark> =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\n    res.end('Express About!\\n');\n});  \n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-blue-color\">app.get('\/random.text', function (req, res)<\/mark> {\n    res.send('Express random.text');\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","protected":false},"excerpt":{"rendered":"<p>express \ubaa8\ub4c8\uc740 \ucee4\ub9e8\ub4dc\ub77c\uc778\uc5d0\uc11c npm \uba85\ub839\uc5b4\ub85c \uc124\uce58 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\ube0c\ub77c\uc6b0\uc800 \uc8fc\uc18c\ub97c \ud1b5\ud574\uc11c \uc6f9\uc0ac\uc774\ud2b8\uc5d0 \uc811\uc18d \ud560 \ub54c \ub3c4\uba54\uc778 \uc8fc\uc18c \uc5d4\ub4dc \ud3ec\uc778\ud2b8\uc5d0 \ub530\ub77c \uc6f9\uc11c\ubc84\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \ub2e4\ub978 \ub0b4\uc6a9\uc744 \ubcf4\uc5ec \uc904 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\uc608\ub97c \ub4e4\uc5b4\uc11c http:\/\/freelifemakers.org\/wordpress \ub77c\uace0 \ud55c\ub2e4\uba74 \uc774 &#8216;\/wordpress&#8217; \ubd80\ubd84\uc744 \uc5d4\ub4dc\ud3ec\uc778\ud2b8\ub77c\uace0 \ud569\ub2c8\ub2e4. \ub2e4\ub978 \ub9d0\ub85c \ub77c\uc6b0\ud2b8 \ub610\ub294 \ub8e8\ud2b8\ub77c\uace0 \uc815\uc758 \ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub807\uac8c \uc5d4\ub4dc\ud3ec\uc778\ud2b8\uc5d0 \ub530\ub77c \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0 \ub2e4\ub978 \uc751\ub2f5\uc744 \ud558\ub294 \uae30\ub2a5\uc744 \ub77c\uc6b0\ud305\uc774\ub77c\uace0 [&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-462","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\/462","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=462"}],"version-history":[{"count":5,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/462\/revisions\/468"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}