{"id":718,"date":"2024-08-16T07:45:45","date_gmt":"2024-08-16T07:45:45","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=718"},"modified":"2024-08-16T09:37:10","modified_gmt":"2024-08-16T09:37:10","slug":"nodejsejs-using-ejs-code-explain-2","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/08\/16\/nodejsejs-using-ejs-code-explain-2\/","title":{"rendered":"[nodejs]EJS\uc0ac\uc6a9\ud558\uae30-\ucf54\ub4dc\uc124\uba85\/Using EJS \u2013 Code Explanation &#8211; 2"},"content":{"rendered":"\n<p>\ubc14\ub85c \uc774\uc804 \ud3ec\uc2a4\ud2b8\uc758 Server.js \ubc0f test.ejs \ucf54\ub4dc \uc124\uba85\uc785\ub2c8\ub2e4.<br>This is the explanation of the Server.js and test.ejs code from the previous post.<\/p>\n\n\n\n<p><strong>1.\ubaa8\ub4c8\ucd94\uac00 \ubd80\ubd84(Module addition part)<\/strong><br>&#8211; ejs\uc640 express,path\ubaa8\ub4c8\uc744 \ucd94\uac00\ud569\ub2c8\ub2e4.<br>&#8211; Add ejs, express, and path modules.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { createServer } = require('http');\nconst ejs = require('ejs'); \/\/ejs\nconst express = require('express'); \/\/express\nconst path = require('path'); \/\/path\nconst app = express();\nconst server = createServer(app);<\/code><\/pre>\n\n\n\n<p><strong>2.EJS \uc14b\ud305(EJS Settings)<\/strong><br>&#8211; view engine\uc73c\ub85c ejs\ub97c \uc0ac\uc6a9\ud558\uace0 ejs\ub514\ub809\ud1a0\ub9ac \uc774\ub984\uc744 &#8216;public&#8217;\uc73c\ub85c \uc9c0\uc815\ud569\ub2c8\ub2e4.<br>&#8211; Use ejs as the view engine and name the ejs directory &#8216;public&#8217;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.set('view engine','ejs');\napp.set('views','.\/public');<\/code><\/pre>\n\n\n\n<p><strong>3.Server static files<\/strong><br>&#8211; express\uc5d0\uc11c \uc0ac\uc6a9\ub420 \ud3f4\ub354 \uc704\uce58 \uc9c0\uc815\ud558\uace0 \uc774\ub984\uc744 public\uc73c\ub85c \uc9c0\uc815 \ud569\ub2c8\ub2e4.<br>&#8211; Specify the location of the folder to be used in express and name it public.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.use(express.static(path.join(__dirname, \"public\")));<\/code><\/pre>\n\n\n\n<p><strong>4.Express get<\/strong><br>&#8211;  \ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c www.yourdamin.xx:3000\/test \uc2e4\ud589\uc2dc \ube0c\ub77c\uc6b0\uc800\uc5d0 Hello Wold!\ucd9c\ub825 \ud569\ub2c8\ub2e4.<br>&#8211; When you run www.yourdamin.xx:3000\/test in your browser, Hello Wold! is displayed in the browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const hello = \"Hello world!\";\napp.get(\"\/test\", (req, res) =&gt; {\n  res.render('test', { message:hello });\n});<\/code><\/pre>\n\n\n\n<p><strong>5.\uc11c\ubc84\uc2e4\ud589 \ucf54\ub4dc<\/strong><br>&#8211; node server.js\uc2e4\ud589\uc2dc &#8216;Server is running on port 3000&#8217;\uc744 \ud45c\uc2dc\ud569\ub2c8\ub2e4.<br>&#8211; \ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c 3000\ubc88\ud3ec\ud2b8\ub85c \uc811\uc18d\ud5c8\uc6a9 \ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<p>When running &#8216;node server.js&#8217; in the shell, &#8216;Server is running on port 3000&#8217; is displayed.<br>Allow connection to port 3000 in the browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const PORT = process.env.PORT || 3000;\nserver.listen(PORT, () =&gt; {\n  console.log(`Server is running on port ${PORT}`);\n});<\/code><\/pre>\n\n\n\n<p><strong>6.\uc804\uccb4\ucf54\ub4dc(full code)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/server.js --&gt; Commonjs type\nconst { createServer } = require('http');\nconst ejs = require('ejs'); \/\/ejs\nconst express = require('express'); \/\/express\nconst path = require('path'); \/\/path\nconst app = express();\nconst server = createServer(app);\n\n\/\/ ejs settings\napp.set('view engine','ejs');\napp.set('views','.\/public');\n\n\/\/ Serve static files from the 'public' directory\napp.use(express.static(path.join(__dirname, \"public\")));\n\n\/\/-----------------------------------------------\n\nconst hello = \"Hello world!\";\napp.get(\"\/test\", (req, res) =&gt; {\n  res.render('test', { message:hello });\n});\n\n\/\/-----------------------------------------------\n\n\n\/\/ Start the server\nconst PORT = process.env.PORT || 3000;\nserver.listen(PORT, () =&gt; {\n  console.log(`Server is running on port ${PORT}`);\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ubc14\ub85c \uc774\uc804 \ud3ec\uc2a4\ud2b8\uc758 Server.js \ubc0f test.ejs \ucf54\ub4dc \uc124\uba85\uc785\ub2c8\ub2e4.This is the explanation of the Server.js and test.ejs code from the previous post. 1.\ubaa8\ub4c8\ucd94\uac00 \ubd80\ubd84(Module addition part)&#8211; ejs\uc640 express,path\ubaa8\ub4c8\uc744 \ucd94\uac00\ud569\ub2c8\ub2e4.&#8211; Add ejs, express, and path modules. 2.EJS \uc14b\ud305(EJS Settings)&#8211; view engine\uc73c\ub85c ejs\ub97c \uc0ac\uc6a9\ud558\uace0 ejs\ub514\ub809\ud1a0\ub9ac \uc774\ub984\uc744 &#8216;public&#8217;\uc73c\ub85c \uc9c0\uc815\ud569\ub2c8\ub2e4.&#8211; Use ejs as the view engine and name the ejs [&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,1],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-nodejs","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/718","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=718"}],"version-history":[{"count":10,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/718\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}