{"id":444,"date":"2024-04-20T01:34:16","date_gmt":"2024-04-20T01:34:16","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=444"},"modified":"2024-04-20T03:13:59","modified_gmt":"2024-04-20T03:13:59","slug":"nodejs-basic-httpserveresmcommonjs","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/04\/20\/nodejs-basic-httpserveresmcommonjs\/","title":{"rendered":"NODEJS\uae30\ubcf8 http server ,NODEJS Basic httpserver(ESM,COMMONJS)"},"content":{"rendered":"\n<p>nodejs\uc5d0\uc11c esm\uacfc commonjs \ubc29\uc2dd\uc758 http  server code\uc785\ub2c8\ub2e4.<br>nodejs\ub294 \uc6f9\uc11c\ubc84(http server)\uac00 \uc5c6\uc2b5\ub2c8\ub2e4.\ucf54\ub4dc\ub85c \uc6f9\uc11c\ubc84\uc758 \uae30\ub2a5\uc744 \uad6c\ud604\ud569\ub2c8\ub2e4.<br>\uae30\ubcf8\uc801\uc73c\ub85c nodejs\ub294 \ud3ec\ud2b8\ubc88\ud638 3000\ubc88\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4 .<br>\uadf8\ub798\uc11c nodejs\ud328\ud0a4\uc9c0\uc778 react\ub098 express\ub9cc\uc73c\ub85c \uc6f9\uc0ac\uc774\ud2b8\ub97c \ub9cc\ub4e0\ub2e4\uba74 <br>\ub9ac\ubc84\uc2a4 \ud504\ub85d\uc2dc\uae30\ub2a5\uc774 \uc788\ub294 Nginx\uac00 \uc88b\uc740 \uc870\ud569\uc774 \ub418\ub9ac\ub77c \uc0dd\uac01\ub429\ub2c8\ub2e4.<br><br>This is ESM and commonjs style http server code in nodejs.<br>nodejs does not have a web server (http server). It implements the functions of a web server through code.<br>By default, nodejs uses port number 3000.<br>So, if you create a website with only the nodejs package react or express,<br>I think Nginx with reverse proxy function would be a good combination.<\/p>\n\n\n\n<p><br>1.ESM,COMMONJS<br>&#8211; nodejs\uc5d0\uc11c\ub294 \ubaa8\ub4c8\uc0ac\uc6a9 \ubc29\uc2dd\uc73c\ub85c ESM\uacfc COMMONJS\ub77c\ub294 \ubc29\uc2dd\uc774 \uc788\uc2b5\ub2c8\ub2e4.<br>&#8211;  ESM\uc774 \uac1c\uc120\ub41c \ubc29\uc2dd\uc774\uace0 COMMONJS\uac00 \uc774\uc804 \ubc29\uc2dd \uc785\ub2c8\ub2e4. <\/p>\n\n\n\n<p>In nodejs, there are two ways to use modules: ESM and COMMONJS.<br>ESM is an improved method and COMMONJS is the previous method.<\/p>\n\n\n\n<p>1)ESM<br>&#8211; \ud30c\uc77c \ud655\uc7a5\uc790\uac00 mjs\uc785\ub2c8\ub2e4. <br>&#8211; js\ud30c\uc77c\uc744 esm\ubc29\uc2dd\uc73c\ub85c \uc0ac\uc6a9\ud560 \uacbd\uc6b0 package.json\ud30c\uc77c\uc5d0\uc11c &#8220;Type&#8221;:&#8221;module&#8221;, \uc744 \ucd94\uac00\ud569\ub2c8\ub2e4.<br>&#8211; \uc544\ub798\uc758 \ucf54\ub4dc\uc5d0\uc11c \uac15\uc870\ub41c \ub179\uc0c9\ubd80\ubd84\uc774 ESM \ubc29\uc2dd\uc758 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>&#8211; \ubcf4\ub77c\uc0c9 \ucf54\ub4dc \ub300\uc2e0 \ub178\ub780\uc0c9 \ucf54\uba58\ud2b8 \ucc98\ub9ac\ub41c \ucf54\ub4dc\ub97c \uc0ac\uc6a9\ud574\ub3c4 \ub429\ub2c8\ub2e4.<\/p>\n\n\n\n<p>-The file extension is mjs.<br>-When using js files in esm format, add &#8220;Type&#8221;:&#8221;module&#8221;, in the package.json file.<br>-The green part highlighted in the code below is the ESM type code.<br>-You can use the yellow commented code instead of the purple code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ server.mjs --&gt; ESM TYPE\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">import { createServer } from 'node:http';<\/mark>\n\nconst server = createServer((req, res) =&gt; {\n  res.writeHead(200, { 'Content-Type': 'text\/plain' });\n  res.end('ESM:Hello World!\\n');\n});\n\n\n\/\/ starts a simple http server locally on port 3000\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#d5a701\" class=\"has-inline-color\">\/*\nserver.listen(3000, '127.0.0.1', () =&gt; {\n  console.log('ESM:Listening on 127.0.0.1:3000');\n});\n*\/<\/mark>\n\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#de11dc\" class=\"has-inline-color\">const PORT = process.env.PORT || 3000;\nserver.listen(PORT, () =&gt; {\n  console.log(`ESM:Server is running on port ${PORT}`);\n});<\/mark><\/code><\/pre>\n\n\n\n<p>-\uc704\uc758 \ucf54\ub4dc\ub97c server.mjs\ud30c\uc77c\uc774 \uc544\ub2cc server.js\ud30c\uc77c\ub85c \uc0ac\uc6a9\ud558\uae30 \uc6d0\ud55c\ub2e4\uba74 package.json\ud30c\uc77c\uc5d0 &#8216; &#8220;Type&#8221;:&#8221;module&#8221;, &#8216;\uc744 \ub2e4\uc74c\uacfc \uac19\uc774 \ucd94\uac00\ud569\ub2c8\ub2e4.<br>-package.json\ud30c\uc77c\uc740 command line\uc5d0\uc11c npm\uba85\ub839\uc73c\ub85c \ubaa8\ub4c8\uc744 \uc124\uce58\ud558\uba74 \uc790\ub3d9\uc73c\ub85c \uc0dd\uc131\ub429\ub2c8\ub2e4.<br><br>-If you want to use the above code in a server.js file rather than a server.mjs file, add &#8216; &#8220;Type&#8221;:&#8221;module&#8221;, &#8216; to the package.json file as follows.<br>-The package.json file is automatically created when you install the module using the npm command from the command line.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"251\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/04\/module.png\" alt=\"\" class=\"wp-image-449\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/04\/module.png 494w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/04\/module-300x152.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<p>2)COMMONJS<br>-\ud30c\uc77c\ud655\uc7a5\uc790\uac00 js\uc785\ub2c8\ub2e4.<br>-nodejs\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 \uae30\ubcf8\uc801\uc778 \ubaa8\ub4c8 \uc0ac\uc6a9\ubc29\uc2dd\uc785\ub2c8\ub2e4. \ud2b9\ubcc4\ud55c \uc14b\ud305\uc774 \ud544\uc694 \ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>-\uc544\ub798\uc758 \ucf54\ub4dc\uc5d0\uc11c \uac15\uc870\ub41c \ub179\uc0c9\ubd80\ubd84\uc774 COMMONJS \ubc29\uc2dd\uc758 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>-\ubcf4\ub77c\uc0c9 \ubd80\ubd84\uc740 COMMONJS\ub97c \uc0ac\uc6a9\ud574\uc11c \ubc14\ub010 \ubd80\ubd84\uc785\ub2c8\ub2e4.<\/p>\n\n\n\n<p>-The file extension is js.<br>-This is the basic module usage method provided by nodejs. No special settings are required.<br>The purple part is the part that was changed using COMMONJS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/server.js --&gt; Commonjs type\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">const http = require('http');<\/mark>\n\nconst server = <mark style=\"background-color:rgba(0, 0, 0, 0);color:#de11dc\" class=\"has-inline-color\">http.createServer<\/mark>((req, res) =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\n    res.end('CommonJS:Hello World!\\n');\n  });\n\n\n\/\/ Start the server\nconst PORT = process.env.PORT || 3000;\nserver.listen(PORT, () =&gt; {\n  console.log(`CommonJS:Server is running on port ${PORT}`);\n});\n<\/code><\/pre>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/www.facebook.com\/plugins\/video.php?height=476&#038;href=https%3A%2F%2Fwww.facebook.com%2Fgideonslife01%2Fvideos%2F369843828699700%2F&#038;show_text=false&#038;width=267&#038;t=0\" width=\"267\" height=\"476\" style=\"border:none;overflow:hidden\" scrolling=\"no\" frameborder=\"0\" allowfullscreen=\"true\" allow=\"autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share\" allowFullScreen=\"true\"><\/iframe>\n","protected":false},"excerpt":{"rendered":"<p>nodejs\uc5d0\uc11c esm\uacfc commonjs \ubc29\uc2dd\uc758 http server code\uc785\ub2c8\ub2e4.nodejs\ub294 \uc6f9\uc11c\ubc84(http server)\uac00 \uc5c6\uc2b5\ub2c8\ub2e4.\ucf54\ub4dc\ub85c \uc6f9\uc11c\ubc84\uc758 \uae30\ub2a5\uc744 \uad6c\ud604\ud569\ub2c8\ub2e4.\uae30\ubcf8\uc801\uc73c\ub85c nodejs\ub294 \ud3ec\ud2b8\ubc88\ud638 3000\ubc88\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4 .\uadf8\ub798\uc11c nodejs\ud328\ud0a4\uc9c0\uc778 react\ub098 express\ub9cc\uc73c\ub85c \uc6f9\uc0ac\uc774\ud2b8\ub97c \ub9cc\ub4e0\ub2e4\uba74 \ub9ac\ubc84\uc2a4 \ud504\ub85d\uc2dc\uae30\ub2a5\uc774 \uc788\ub294 Nginx\uac00 \uc88b\uc740 \uc870\ud569\uc774 \ub418\ub9ac\ub77c \uc0dd\uac01\ub429\ub2c8\ub2e4. This is ESM and commonjs style http server code in nodejs.nodejs does not have a web server (http server). It implements the functions of [&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-444","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\/444","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=444"}],"version-history":[{"count":8,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}