Framework/Express

Node.js / Express.js์—์„œ Router ์กฐ์ž‘ํ•˜๊ธฐ

์„œ๋ฆฌโ˜… 2024. 7. 28. 16:25

๐Ÿš€ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ

 

๐Ÿ’พ Visual Studio Code

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

 

๐Ÿ’พ Node.js

 

Node.js — Run JavaScript Everywhere

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

 

๐Ÿ’พ 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;