{"id":12385,"date":"2022-07-09T19:07:40","date_gmt":"2022-07-09T10:07:40","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=12385"},"modified":"2022-07-09T19:12:06","modified_gmt":"2022-07-09T10:12:06","slug":"node-js%ec%9d%98-get-post-%ec%b2%98%eb%a6%ac","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=12385","title":{"rendered":"Node.js\uc758 GET, POST \ucc98\ub9ac"},"content":{"rendered":"<p>\uba3c\uc800 express\ub97c \uc774\uc6a9\ud574 \ub2e4\uc74c\uacfc \uac19\uc774 \uc11c\ubc84\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst express = require(\"express\");\r\nconst app = express();\r\n\r\nconst table = [\r\n    { name: \"Jack\", alias: \"Monkey\" },\r\n    { name: \"Shelly\", alias: \"Cat\" },\r\n    { name: \"Toms\", alias: \"Dog\" }\r\n];\r\n\r\n\/\/ \uc5ec\uae30\uc5d0 GET, POST\ub97c \ucc98\ub9ac\ud558\ub294 \ucf54\ub4dc\uac00 \ucd94\uac00\ub428\r\n\r\napp.use(express.static(__dirname + \"\/static\"));\r\napp.listen(3000);\r\n<\/pre>\n<p>4\ubc88 \ucf54\ub4dc\uc758 table\uc740 DB \ub300\uc2e0 \uc0ac\uc6a9\ud558\ub294 \ub370\uc774\ud130 \uac1d\uccb4\uc785\ub2c8\ub2e4. \uadf8\ub9ac\uace0 10\ubc88 \ucf54\ub4dc\ub294 static\uc774\ub77c\ub294 \ub514\ub809\ud1a0\ub9ac\uc5d0 test.html \ud30c\uc77c\uc744 \ub123\uc744 \uac83\uc778\ub370.. \uc774\ub97c \uc6f9\ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c 127.0.0.1:3000\/test.html \ucc98\ub7fc \uc811\uadfc\ud560 \uc218 \uc788\ub3c4\ub85d \ud558\uae30 \uc704\ud568\uc785\ub2c8\ub2e4. \uc774 test.html \ud30c\uc77c\uc774 \uc6b0\ub9ac\uac00 \ub9cc\ub4e4 \uc11c\ubc84\uc5d0 GET, POST \uc694\uccad\uc744 \ud558\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0 \ud574\ub2f9\ud558\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n<p>\uac00\uc7a5 \uba3c\uc800 table\uc5d0 \uc800\uc7a5\ub41c \uc0ac\uc6a9\uc790\uc758 name \ubaa9\ub85d\uc744 \uc5bb\uc5b4\uc624\ub294 names GET \ucc98\ub9ac\uc5d0 \ub300\ud55c \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4. \ucf54\ub4dc \ucd94\uac00\ub294 10\ubc88\uc5d0 \ud558\uba74 \ub429\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\napp.get(\"\/names\", (req, res) => {\r\n    const names = table.map(item => {\r\n        return { name: item.name };\r\n    });\r\n    res.status(200).json(names);\r\n});\r\n<\/pre>\n<p>test.html\uc5d0 \uc785\ub825\ud560 \ud074\ub77c\uc774\uc5b8\ud2b8 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uad6c\uc694.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst url = \"http:\/\/127.0.0.1:3000\/names\";\r\nfetch(url, {\r\n    method: \"GET\"\r\n}).then(response => {\r\n    return response.text();\r\n}).then(text => {\r\n    const data = JSON.parse(text);\r\n    console.log(data);\r\n}).catch(error => {\r\n    console.warn(error);\r\n});\r\n<\/pre>\n<p>\uc6f9\ube0c\ub77c\uc6b0\uc800\uc758 \ucf58\uc194\uc5d0 \ud45c\uc2dc\ub418\ub294 \uc2e4\ud589 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>(3) [{\u2026}, {\u2026}, {\u2026}]<br \/>\n0: {name: 'Jack'}<br \/>\n1: {name: 'Shelly'}<br \/>\n2: {name: 'Toms'}<br \/>\n...<\/code><\/p>\n<p>\uc774\ubc88\uc5d0\ub294 Query String\uc744 \uac16\ub294 GET \uc694\uccad\uc785\ub2c8\ub2e4. \uc774\ub984\uc744 \ubc1b\uc544 \ubcc4\uce6d\uc744 \uadf8 \uacb0\uacfc\ub85c \uc804\ub2ec\ud558\ub294 \ucc98\ub9ac\uc5d0 \ub300\ud55c \uc11c\ubc84 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\napp.get(\"\/alias\", (req, res) => {\r\n    const name = req.query.name;\r\n    const result = table.find((item) => {\r\n        return item.name === name;\r\n    });\r\n\r\n    if(result) {\r\n        res.status(200).json({ alias: result.alias });\r\n    } else {\r\n        res.status(200).json({});\r\n    }\r\n});\r\n<\/pre>\n<p>\ud074\ub77c\uc774\uc5b8\ud2b8 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst url = \"http:\/\/127.0.0.1:3000\/alias?name=Suzan\";\r\nfetch(url, {\r\n    method: \"GET\",\r\n}).then(response => {\r\n    return response.text();\r\n}).then(text => {\r\n    const data = JSON.parse(text);\r\n    console.log(data);\r\n}).catch(error => {\r\n        console.warn(error);\r\n});\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>{alias: 'Cat'}<\/code><\/p>\n<p>\uc774\uc81c POST \ud638\ucd9c\uc785\ub2c8\ub2e4. \uc11c\ubc84\uc5d0 \uc0c8\ub85c\uc6b4 \uc0ac\ub78c\uc744 \ucd94\uac00\ud558\ub294 \uc11c\ubc84\uce21 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\napp.use(express.json());\r\n\r\napp.post(\"\/add\", (req, res) => {\r\n    const item = req.body;\r\n\r\n    if(item.name && item.alias) {\r\n        table.push(item);\r\n        res.sendStatus(200);\r\n    } else {\r\n        res.sendStatus(400);\r\n    }\r\n});\r\n<\/pre>\n<p>\uc704\uc758 \uc11c\ube44\uc2a4\ub97c \uc774\uc6a9\ud558\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst url = \"http:\/\/127.0.0.1:3000\/add\";\r\nfetch(url, {\r\n    method: \"POST\",\r\n    headers: { \"Content-Type\": \"application\/json\" },\r\n    body: JSON.stringify({ name: \"Suzan\", alias: \"\uace0\uc2b4\ub3c4\uce58\" })\r\n}).then(response => {\r\n    console.log(response.ok);\r\n}).catch(error => {\r\n    console.warn(error);\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uba3c\uc800 express\ub97c \uc774\uc6a9\ud574 \ub2e4\uc74c\uacfc \uac19\uc774 \uc11c\ubc84\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4. const express = require(&#8220;express&#8221;); const app = express(); const table = [ { name: &#8220;Jack&#8221;, alias: &#8220;Monkey&#8221; }, { name: &#8220;Shelly&#8221;, alias: &#8220;Cat&#8221; }, { name: &#8220;Toms&#8221;, alias: &#8220;Dog&#8221; } ]; \/\/ \uc5ec\uae30\uc5d0 GET, POST\ub97c \ucc98\ub9ac\ud558\ub294 \ucf54\ub4dc\uac00 \ucd94\uac00\ub428 app.use(express.static(__dirname + &#8220;\/static&#8221;)); app.listen(3000); 4\ubc88 \ucf54\ub4dc\uc758 table\uc740 DB \ub300\uc2e0 \uc0ac\uc6a9\ud558\ub294 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=12385\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Node.js\uc758 GET, POST \ucc98\ub9ac&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[],"class_list":["post-12385","post","type-post","status-publish","format-standard","hentry","category-node-js"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/12385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12385"}],"version-history":[{"count":6,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/12385\/revisions"}],"predecessor-version":[{"id":12391,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/12385\/revisions\/12391"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12385"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}