{"id":1142,"date":"2025-02-21T02:08:34","date_gmt":"2025-02-21T02:08:34","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=1142"},"modified":"2025-04-18T09:45:16","modified_gmt":"2025-04-18T09:45:16","slug":"nodejs4","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/02\/21\/nodejs4\/","title":{"rendered":"nodejs4 &#8211; GET,POST METHOD \/ GET,POST \ub370\uc774\ud130 \uc804\uc1a1\ubc29\uc2dd"},"content":{"rendered":"\n<p>GET,POST METHOD \/ GET,POST \ub370\uc774\ud130 \uc804\uc1a1\ubc29\uc2dd<\/p>\n\n\n\n<p>package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"dependencies\": {\n    \"body-parser\": \"^1.20.3\",\n    \"ejs\": \"^3.1.10\",\n    \"express\": \"^4.21.2\",\n    \"path\": \"^0.12.7\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>server.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*---------------------------------------------------------------------------\n\n- NODEJS-4 : get method\n- 2025.02.\n\n---------------------------------------------------------------------------*\/\n\n\/\/ module : https:\/\/www.npmjs.com\nconst ejs = require('ejs');              \/\/ https:\/\/ejs.co\nconst path = require('path');            \/\/ https:\/\/nodejs.org\/docs\/latest\/api\/path.html\nconst express = require('express');      \/\/ https:\/\/expressjs.com\/\n\nconst app = express();\nconst port = 3000; \n\n\/\/ ejs\napp.set('view engine','ejs');\napp.set('views','.\/public');\n\n\/\/ static directory\n\/\/ app.use(express.static(path.join(__dirname, \"public\")));\n\n\/*---------------------------------------------------------------------------\n  Start route\n---------------------------------------------------------------------------*\/\n\napp.get('\/', (req, res) =&gt; {\n  \/\/res.send('Hello, World!');\n  res.sendFile(path.join(__dirname, \"\/html\", \"\/index.html\")); \/\/ html directory,index.html\n\n  console.log(\"log:index1.html\");\n  console.log(`log: filename : ${__filename}`); \/\/ server.js\n  console.log(`log: path:${__dirname}`); \/\/ directory path\n});\n\napp.get('\/view1', (req, res) =&gt; {\n  res.render('view1'); \/\/ ejs file\n});\n\napp.get('\/get1', (req, res) =&gt; {\n    const id = req.query.id;\n    const name = req.query.name;\n\n    console.log(`id: ${id}`)\n    console.log(`name : ${name}`)\n    res.render('get1',{fid: id,fname: name}); \n});\n\n\n\/*---------------------------------------------------------------------------\n  Start Server\n---------------------------------------------------------------------------*\/\n\n\/\/ Start the server\napp.listen(port, () =&gt; {\n  console.log(`Server listening at http:\/\/localhost:${port}`);\n});\n\n<\/code><\/pre>\n\n\n\n<p>server2.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*---------------------------------------------------------------------------\n\n- NODEJS-4 : get,post method\n- 2025.02.\n\n---------------------------------------------------------------------------*\/\n\n\/\/ module : https:\/\/www.npmjs.com\nvar bodyParser = require('body-parser'); \/\/ POST,https:\/\/github.com\/expressjs\/body-parser#readme\nconst ejs = require('ejs');              \/\/ https:\/\/ejs.co\nconst path = require('path');            \/\/ https:\/\/nodejs.org\/docs\/latest\/api\/path.html\nconst express = require('express');      \/\/ https:\/\/expressjs.com\/\n\nconst app = express();\nconst port = 3000; \n\n\/\/ ejs\napp.set('view engine','ejs');\napp.set('views','.\/public');\n\n\/\/ static directory\n\/\/ app.use(express.static(path.join(__dirname, \"public\")));\n\n\/\/ post method\napp.use(express.urlencoded({ extended: true }));  \/\/ For form post method\napp.use(express.json()); \/\/ For parsing JSON data\n\n\/*---------------------------------------------------------------------------\n  Start route\n---------------------------------------------------------------------------*\/\n\napp.get('\/', (req, res) =&gt; {\n  \/\/res.send('Hello, World!');\n  res.sendFile(path.join(__dirname, \"\/html\", \"\/index.html\"));\n\n  console.log(\"log:index1.html\");\n  console.log(`log: filename : ${__filename}`); \/\/ server.js\n  console.log(`log: path:${__dirname}`); \/\/ directory path\n});\n\napp.get('\/view1', (req, res) =&gt; {\n  res.render('view1'); \/\/ ejs file\n});\n\napp.get('\/get1', (req, res) =&gt; {\n    const id = req.query.id; \n    console.log(`id: ${id}`)\n    res.render('get1',{fid: id}); \/\/ ejs file\n});\n\n\/\/---------------------------------------------------------------\n\napp.get('\/form', (req, res) =&gt; {\n  res.send(`\n      &lt;form method=\"POST\" action=\"\/formProc\"&gt;\n          &lt;label for=\"name\"&gt;Name:&lt;\/label&gt;\n          &lt;input type=\"text\" id=\"name\" name=\"name\"&gt;&lt;br&gt;&lt;br&gt;\n\n          &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n          &lt;input type=\"email\" id=\"email\" name=\"email\"&gt;&lt;br&gt;&lt;br&gt;\n          &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n      &lt;\/form&gt;\n  `);\n});\n\napp.post(\"\/formProc\", (req, res) =&gt; {\n    \n    const name = req.body.name;\n    const email = req.body.email;\n\n    console.log(`form name : ${name}`)\n    console.log(`form email : ${email}`)\n    res.render('post1',{fname: name,femail: email});\n});\n\n\/\/---------------------------------------------------------------\napp.get('\/formapi', (req, res) =&gt; {\n  res.send(`\n      &lt;form method=\"POST\" action=\"\/api\"&gt;\n          &lt;label for=\"api\"&gt;API&lt;\/label&gt;&lt;br&gt;&lt;br&gt;\n          &lt;label for=\"name\"&gt;Name:&lt;\/label&gt;\n          &lt;input type=\"text\" id=\"name\" name=\"name\"&gt;&lt;br&gt;&lt;br&gt;\n\n          &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n          &lt;input type=\"email\" id=\"email\" name=\"email\"&gt;&lt;br&gt;&lt;br&gt;\n          &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n      &lt;\/form&gt;\n  `);\n});\n\n\napp.post('\/api', (req, res) =&gt; {\n  console.log(\"Received JSON data:\", req.body);\n\n  res.json({ message: \"Data received successfully!\", data: req.body }); \/\/ Send JSON response\n});\n\n\n\/*---------------------------------------------------------------------------\n  Start Server\n---------------------------------------------------------------------------*\/\n\n\/\/ Start the server\napp.listen(port, () =&gt; {\n  console.log(`Server listening at http:\/\/localhost:${port}`);\n});\n\n<\/code><\/pre>\n\n\n\n<p>\/html\/index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3&gt;index.html&lt;\/h3&gt;\n<\/code><\/pre>\n\n\n\n<p>\/public\/get1.ejs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3&gt;get1.ejs&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;fid:&lt;%=fid%&gt;&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;fname:&lt;%=fname%&gt;&lt;\/h3&gt;<\/code><\/pre>\n\n\n\n<p>\/public\/post1.ejs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3&gt;post1.ejs&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;name : &lt;%=fname%&gt;&lt;\/h3&gt;&lt;br&gt;\n&lt;h3&gt;email : &lt;%=femail%&gt;&lt;\/h3&gt;\n<\/code><\/pre>\n\n\n\n<p>\/public\/view1.ejs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3&gt;view1.ejs&lt;\/h3&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GET,POST METHOD \/ GET,POST \ub370\uc774\ud130 \uc804\uc1a1\ubc29\uc2dd package.json server.js server2.js \/html\/index.html \/public\/get1.ejs \/public\/post1.ejs \/public\/view1.ejs<\/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-1142","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\/1142","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=1142"}],"version-history":[{"count":3,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1142\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1142\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}