{"id":12393,"date":"2022-07-09T21:12:45","date_gmt":"2022-07-09T12:12:45","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=12393"},"modified":"2022-07-09T21:24:55","modified_gmt":"2022-07-09T12:24:55","slug":"node-js%ec%97%90%ec%84%9c-postgresql-%ec%97%b0%ea%b2%b0","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=12393","title":{"rendered":"Node.js\uc5d0\uc11c PostgreSQL \uc5f0\uacb0"},"content":{"rendered":"<p>\uc774 \uae00\uc740 \uc544\ub798\uc758 \ud3ec\uc2a4\ud2b8 \ub0b4\uc6a9\uc5d0 \ub300\ud574 DBMS\ub97c \uc774\uc6a9\ud574 \uc7ac\uc791\uc131\ub418\uc5c8\uc2b5\ub2c8\ub2e4.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"bgifO4hqCb\"><p><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=12385\">Node.js\uc758 GET, POST \ucc98\ub9ac<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Node.js\uc758 GET, POST \ucc98\ub9ac&#8221; &#8212; GIS Developer\" src=\"http:\/\/www.gisdeveloper.co.kr\/?p=12385&#038;embed=true#?secret=5MeCdyj9M7#?secret=bgifO4hqCb\" data-secret=\"bgifO4hqCb\" width=\"525\" height=\"296\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\uc704 \uae00\uc5d0 \ub300\ud574\uc11c \ud074\ub77c\uc5b8\ud2b8\uc758 \ucf54\ub4dc\ub294 \ubaa8\ub450 \ub3d9\uc77c\ud558\uace0 \uc11c\ubc84\uc758 \ucf54\ub4dc\ub9cc \ubcc0\uacbd\ub418\uc5c8\uc2b5\ub2c8\ub2e4. \uc11c\ubc84\uc5d0 \ub300\ud55c \uc804\uccb4 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst express = require(\"express\");\r\nconst app = express();\r\n\r\nconst { Client } = require(\"pg\");\r\n\r\nconst dbClient = new Client({\r\n    user: \"postgres\",\r\n    host: \"localhost\",\r\n    database: \"postgres\",\r\n    password: \"****\",\r\n    port: 5432\r\n});\r\n\r\ndbClient.connect();\r\n\r\napp.get(\"\/names\", (req, res) => {\r\n    dbClient.query(\"SELECT name FROM names\", (error, result) => {\r\n        if(error) {\r\n            res.sendStatus(500);\r\n        } else {\r\n            res.status(200).json(result.rows);\r\n        }\r\n    });    \r\n});\r\n\r\napp.get(\"\/alias\", (req, res) => {\r\n    const name = req.query.name;\r\n\r\n    dbClient.query(`SELECT alias FROM names WHERE name = '${name}'`, (error, result) => {\r\n        if(error) {\r\n            res.sendStatus(500);\r\n        } else {\r\n            res.status(200).json(result.rows);\r\n        }\r\n    });\r\n});\r\n\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        dbClient.query(`INSERT INTO names (name, alias) VALUES ('${item.name}', '${item.alias}');`, \r\n            (error, result) => {\r\n                if(result) {\r\n                    console.log(`Changed Row Count ${result.rowCount}`);\r\n                    res.sendStatus(200);\r\n                } else {\r\n                    res.sendStatus(500);           \r\n                }\r\n            }\r\n        );\r\n    } else {\r\n         res.sendStatus(400);\r\n    }    \r\n});\r\n\r\napp.use(express.static(__dirname + \"\/static\"));\r\napp.listen(3000);\r\n<\/pre>\n<p>\uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c \ub9cc\uc57d DBMS\uc5d0 \ub300\ud55c Connection Pool\uc744 \ub3c4\uc785\ud558\uace0\uc790 \ud55c\ub2e4\uba74 4~12\ubc88 \ucf54\ub4dc\ub97c \ub2e4\uc74c\ucc98\ub7fc \ubcc0\uacbd\ud558\uae30\ub9cc \ud558\uba74 \ub429\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst { Pool } = require(\"pg\");\r\n\r\nconst dbClient = new Pool({\r\n    user: \"postgres\",\r\n    host: \"localhost\",\r\n    database: \"postgres\",\r\n    password: \"3224\",\r\n    port: 5432\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uc774 \uae00\uc740 \uc544\ub798\uc758 \ud3ec\uc2a4\ud2b8 \ub0b4\uc6a9\uc5d0 \ub300\ud574 DBMS\ub97c \uc774\uc6a9\ud574 \uc7ac\uc791\uc131\ub418\uc5c8\uc2b5\ub2c8\ub2e4. Node.js\uc758 GET, POST \ucc98\ub9ac \uc704 \uae00\uc5d0 \ub300\ud574\uc11c \ud074\ub77c\uc5b8\ud2b8\uc758 \ucf54\ub4dc\ub294 \ubaa8\ub450 \ub3d9\uc77c\ud558\uace0 \uc11c\ubc84\uc758 \ucf54\ub4dc\ub9cc \ubcc0\uacbd\ub418\uc5c8\uc2b5\ub2c8\ub2e4. \uc11c\ubc84\uc5d0 \ub300\ud55c \uc804\uccb4 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4. const express = require(&#8220;express&#8221;); const app = express(); const { Client } = require(&#8220;pg&#8221;); const dbClient = new Client({ user: &#8220;postgres&#8221;, host: &#8220;localhost&#8221;, database: &#8220;postgres&#8221;, password: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=12393\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Node.js\uc5d0\uc11c PostgreSQL \uc5f0\uacb0&#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-12393","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\/12393","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=12393"}],"version-history":[{"count":7,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/12393\/revisions"}],"predecessor-version":[{"id":12401,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/12393\/revisions\/12401"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12393"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}