{"id":731,"date":"2024-08-20T03:04:31","date_gmt":"2024-08-20T03:04:31","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=731"},"modified":"2024-08-20T10:40:25","modified_gmt":"2024-08-20T10:40:25","slug":"nodejs-create-a-module","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/08\/20\/nodejs-create-a-module\/","title":{"rendered":"[nodejs]\ubaa8\ub4c8\ub9cc\ub4e4\uae30 \/ Create a module"},"content":{"rendered":"\n<p>\ubaa8\ub4c8\uc744 \uc0ac\uc6a9\ud558\uba74 \uc880 \ub354 \uac00\ub3c5\uc131\uc774 \uc88b\uc544\uc9d1\ub2c8\ub2e4.<br>Using modules makes it more readable.<\/p>\n\n\n\n<p>\ud2b9\ubcc4\ud788 \uae30\ub2a5\ubcc4\ub85c \ubaa8\ub4c8\uc744 \uad6c\uc131\ud558\uba74 \uc774\ud574\ud558\uae30 \ud3b8\ud569\ub2c8\ub2e4.<br>It is easier to understand if you organize modules by function.<\/p>\n\n\n\n<p>\uc774 \uc2dc\uac04\uc5d0\ub294 mysql\ub370\uc774\ud130\ubca0\uc774\uc2a4\uc5d0 \ub370\uc774\ud130 \uc785\ub825\ud558\uace0 \uac80\uc0c9\ud558\ub294 \ubd80\ubd84\uc744 \ubaa8\ub4c8\ub85c \ub9cc\ub4e4\uc5b4 \ubd05\ub2c8\ub2e4.<br>At this time,we will create a module for entering and retrieving data into the mysql database. <\/p>\n\n\n\n<p>\uba3c\uc800 mysql\uc774 \uc124\uce58\ub418\uc5b4 \uc788\uc5b4\uc57c \ud558\uace0 mysql2\ubaa8\ub4c8\uc774 \uc124\uce58\ub418\uc5b4\uc788\uc5b4\uc57c \ud569\ub2c8\ub2e4.<br>First, the mysql database must be installed and the mysql2 module must be installed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"130\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/mysql2.png\" alt=\"\" class=\"wp-image-767\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/mysql2.png 605w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/mysql2-300x64.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/figure>\n\n\n\n<p><strong>1.user\ud14c\uc774\ube14 \ub9cc\ub4e4\uae30(Create a user table)<\/strong><br>&#8211; usre.sql\ud30c\uc77c \ub9cc\ub4e4\uae30(Create a usre.sql)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"212\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/1.png\" alt=\"\" class=\"wp-image-736\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/1.png 619w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/1-300x103.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure>\n\n\n\n<p>&#8211; mysql \ub370\uc774\ud130\ubca0\uc774\uc2a4\uc5d0 user.sql\uc2e4\ud589(Run user.sql on mysql database)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"53\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/2-1.png\" alt=\"\" class=\"wp-image-739\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/2-1.png 735w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/2-1-300x22.png 300w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<p>&#8211; \ud14c\uc774\ube14 \uc0dd\uc131 \ud655\uc778(Confirm table creation)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"51\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/3.png\" alt=\"\" class=\"wp-image-740\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/3.png 650w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/3-300x24.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"415\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/4.png\" alt=\"\" class=\"wp-image-741\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/4.png 639w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/4-300x195.png 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"204\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/5.png\" alt=\"\" class=\"wp-image-742\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/5.png 541w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/5-300x113.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2.\ucf54\ub4dc\uc791\uc131(Write code)<\/strong><br>&#8211; \ubaa8\ub4c8\ud30c\uc77c \ub9cc\ub4e4\uace0 \ucf54\ub4dc\uc791\uc131(Create a module file and write code)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"263\" height=\"227\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/mysqldb_jsFolder.png\" alt=\"\" class=\"wp-image-771\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/mysqldb.js\n\nconst mysql = require('mysql2'); \/\/ mysql module\n\nasync function userInsert(hostname, dbuser, dbpassword, dbname, userid,password,name) {\n    const connection = mysql.createConnection({\n        host: hostname,\n        user: dbuser,\n        password: dbpassword,\n        database: dbname\n    });\n\n    return new Promise((resolve, reject) =&gt; {\n        var sql = 'INSERT INTO user(userid,password,name) values(?,?,?)';\n        var values = &#91;userid,password,name];\n        connection.query(sql, values, (error, rows) =&gt; {\n            if (error) {\n                reject(error);\n            } else {\n                resolve(rows);\n            }\n        });\n    }).finally(() =&gt; {\n        connection.end((err) =&gt; {\n            if (err) {\n                console.error('Error closing the connection:', err);\n            } else {\n                \n            }\n        });\n    });\n}\nmodule.exports = { userInsert };<\/code><\/pre>\n\n\n\n<p>-server.js\ucf54\ub4dc\uc791\uc131(Write server.js code)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/server.js --&gt; Commonjs type\nconst { createServer } = require('http');\nconst ejs = require('ejs'); \/\/ejs\nconst express = require('express'); \/\/express\nconst path = require('path'); \/\/path\n<strong>const mysql = require('mysql2'); \/\/ mysql<\/strong>\n\nconst app = express();\nconst server = createServer(app);\n\n\/\/ ejs settings\n\/\/-----------------------------------------------\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\/\/ mysql db information\n<strong>const dbname = \"your database name\";\nconst hostname = \"localhost\";\nconst dbuser = \"your database user\";\nconst dbpassword = \"your password\";<\/strong>\n\n\/\/ module import\n<strong>const {\n  userInsert,\n} = require(\".\/mysqldb\");<\/strong>\n\/\/-----------------------------------------------\n\nconst hello = \"Hello world!\";\napp.get(\"\/test\", (req, res) =&gt; {\n  res.render('test', { message:hello });\n});\n\n\/\/------------------------------------------------\n\n<strong>app.get(\"\/user\", (req, res) =&gt; {\n  \n  const hello = \"db insert complete\";\n  try{\n    userInsert(hostname, dbuser, dbpassword, dbname, \"userid1\",\"123456\",\"johnDoe\")\n    res.render('test', { message:hello });\n\n  }catch(error){\n\n    console.error('Error fetching data:', error);\n    res.status(500).send('Internal Server Error');\n  }\n\n});<\/strong>\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});\n<\/code><\/pre>\n\n\n\n<p><strong>3. \ucf54\ub4dc \uc2e4\ud589 \uacb0\uacfc \ud655\uc778(Check code execution results)<\/strong><br>&#8211; server.js\ucf54\ub4dc \uc2e4\ud589 \ubc0f \ube0c\ub77c\uc6b0\uc800 \ud14c\uc2a4\ud2b8(Execute server.js code and test browser)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"43\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/nodeserverstart-2.png\" alt=\"\" class=\"wp-image-751\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"184\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/dbinsertcomplete240802.png\" alt=\"\" class=\"wp-image-752\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/dbinsertcomplete240802.png 500w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/dbinsertcomplete240802-300x110.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>-mysql\uc5d0 \ub370\uc774\ud130 \uc785\ub825\ud655\uc778(Check data entry into mysql)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"189\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/selectuser.png\" alt=\"\" class=\"wp-image-754\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/selectuser.png 650w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/08\/selectuser-300x87.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ubaa8\ub4c8\uc744 \uc0ac\uc6a9\ud558\uba74 \uc880 \ub354 \uac00\ub3c5\uc131\uc774 \uc88b\uc544\uc9d1\ub2c8\ub2e4.Using modules makes it more readable. \ud2b9\ubcc4\ud788 \uae30\ub2a5\ubcc4\ub85c \ubaa8\ub4c8\uc744 \uad6c\uc131\ud558\uba74 \uc774\ud574\ud558\uae30 \ud3b8\ud569\ub2c8\ub2e4.It is easier to understand if you organize modules by function. \uc774 \uc2dc\uac04\uc5d0\ub294 mysql\ub370\uc774\ud130\ubca0\uc774\uc2a4\uc5d0 \ub370\uc774\ud130 \uc785\ub825\ud558\uace0 \uac80\uc0c9\ud558\ub294 \ubd80\ubd84\uc744 \ubaa8\ub4c8\ub85c \ub9cc\ub4e4\uc5b4 \ubd05\ub2c8\ub2e4.At this time,we will create a module for entering and retrieving data into the mysql database. \uba3c\uc800 mysql\uc774 \uc124\uce58\ub418\uc5b4 [&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-731","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\/731","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=731"}],"version-history":[{"count":20,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/731\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/731\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}