๐ ๊ฐ๋ฐ ํ๊ฒฝ
๐พ Visual Studio Code
๐พ Node.js
๐พ Express.js
Express.js ์ค์น ๋ช ๋ น์ด
npm install express
๐พ Nodemon
Nodemon ์ค์น ๋ช ๋ น์ด
npm install nodemon
1. Express.js์์ Router ์ฌ์ฉํ๊ธฐ
Node.js์์ ๋ฉ์ธ ์๋ฒ๊ฐ ๋ชจ๋ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ์๋ ์์ ์ด ๋ง๊ธฐ ๋๋ฌธ์ ๊ฐ๊ฐ ๊ฒฝ๋ก์ ์์ฒญ์ด ๋ค์ด์ค๋ ๊ฒฝ์ฐ์๋ ๋ผ์ฐํฐ๊ฐ ์์ ์ ์ฒ๋ฆฌํ ์ ์๋๋ก ํ๋ค. app.js์์๋ ์ฌ์ฉํ ๋ผ์ฐํฐ๋ง ๋ฑ๋กํ๋ค.
1) Express.js ๊ธฐ๋ณธ ํด๋ ์์ฑ
๐public๊ณผ ๐routes ํด๋๋ฅผ ์์ฑํ๋ค.
2) Router
์๋ ์ฝ๋๋ main ํ์ด์ง์ ์ ์ํ๋ ์์ ์ด๋ค.
๐ project > ๐ app.js
// 1. ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
const express = require("express");
// 2. ๋ผ์ฐํฐ ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
const router = require("./routes/router");
// 3. ์ ํ๋ฆฌ์ผ์ด์
์ค์
const app = express();
const PORT = process.env.PORT || 3000;
// 4. ๋ผ์ฐํฐ ์ค์
app.use("/", router);
// 5. ์๋ฒ ์์
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
๐ public > ๐ main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Main</h1>
</body>
</html>
๐ routes > ๐ router.js
const express = require("express");
const router = express.Router();
// ๋ฉ์ธ ํ์ด์ง
router.get("/", (req, res) => {
res.render("main");
});
module.exports = router;
2. Express.js์์ ์ฌ๋ฌ ๊ฐ์ Router ์ฌ์ฉํ๊ธฐ
1) Express.js ๊ธฐ๋ณธ ํด๋ ์์ฑ
๐public๊ณผ ๐routes ํด๋๋ฅผ ์์ฑํ๋ค.
2) Router
์๋ ์ฝ๋๋ mainRouter์ subRouter๋ฅผ ํตํด ์ฌ๋ฌ ํ์ด์ง์ ์ ์ํ๋ ์์ ์ด๋ค.
mainRouter๋main, football, baseball ํ์ด์ง์ ์ ์ํ ์ ์๊ฒ ํด์ค๋ค.
subRouter๋ minor, fishing ํ์ด์ง์ ์ ์ํ ์ ์๊ฒ ํด์ค๋ค.
๐ Project > ๐ app.js
const express = require("express");
const app = express();
// ๋ผ์ฐํฐ ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
const mainRouter = require("./routes/mainRouter");
const subRouter = require("./routes/subRouter");
// ๊ฒฝ๋ก์ ๋ง๋ ๋ผ์ฐํฐ๋ฅผ ์ค์
app.use("/", mainRouter);
app.use("/minor", subRouter);
app.listen(3000);
๐ Public > ๐ main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Sports</h1>
<a href="football">Football</a>
<a href="baseball">Baseball</a>
<a href="minor">Minor</a>
</body>
</html>
๐ Public > ๐ football.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Football</h1>
</body>
</html>
๐ Public > ๐ baseball.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Baseball</h1>
</body>
</html>
๐ Public > ๐ minor.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Minor</h1>
<a href="/minor/fishing">Fishing</a>
</body>
</html>
๐ Public > ๐ fishing.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Fishing</h1>
</body>
</html>
๐ routes > ๐ mainRouter.js
const express = require("express");
const router = express.Router();
// ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์์ ํ๋ ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
// path.join(๊ฒฝ๋ก, ๊ฒฝ๋ก, ๊ฒฝ๋ก ...) : ๊ฒฝ๋ก๋ฅผ ์ฐ๊ฒฐ
const path = require("path");
const file_path = path.join(__dirname, "../public");
// ๋ฉ์ธ ํ์ด์ง
router.get("/", (req, res)=>{
res.sendFile(file_path + "/main.html");
});
// ์ถ๊ตฌ ํ์ด์ง
router.get("/football", (req, res)=>{
res.sendFile(file_path + "/football.html");
})
// ์ผ๊ตฌ ํ์ด์ง
router.get("/baseball", (req, res)=>{
res.sendFile(file_path + "/baseball.html");
})
module.exports = router;
๐ routes > ๐ subRouter.js
const express = require("express");
const router = express.Router();
const path = require("path");
const file_path = path.join(__dirname, "../public");
// minor ํ์ด์ง์ ๋ฉ์ธ
router.get("/", (req, res)=>{
res.sendFile(file_path + "/minor.html");
});
// ๋์ ํ์ด์ง
router.get("/fishing", (req, res)=>{
res.sendFile(file_path + "/fishing.html")
})
module.exports = router;
'Framework > Express' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Node.js / Express์์ DB ์ฐ๋ํ๊ธฐ (0) | 2024.07.28 |
---|---|
Node.js / Express์์ GET๊ณผ POST ์์ฒญ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ (0) | 2024.07.22 |
ํฌ์คํ ์ด ์ข์๋ค๋ฉด "์ข์์โค๏ธ" ๋๋ "๊ตฌ๋ ๐๐ป" ํด์ฃผ์ธ์!