# Node.js Express router ์‚ฌ์šฉํ•˜๊ธฐ

์‹ค์ œ ์‚ฌ์ดํŠธ๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ app.get(), app.post() ์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์ˆ˜์‹ญ, ์ˆ˜๋ฐฑ๊ฐœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ผ์šฐํŠธ ์ฝ”๋“œ๋“ค์„ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๊ด€๋ฆฌํ•ด ๋ณต์žกํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, express๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ ๋ผ์šฐํŒ…์„ ๊น”๋”ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

# router ๊ฐ์ฒด๋กœ ๋ผ์šฐํŒ… ๋ถ„๋ฆฌ

์ผ๋ฐ˜์ ์œผ๋กœ router ํŒŒ์ผ๋“ค์€ /routes ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ํ•ด๋‹น ์˜ˆ์ œ์—์„œ๋Š” ์ „์ฒด ์„œ๋ฒ„๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” /server.js ํŒŒ์ผ๊ณผ shop ์™€ ๊ด€๋ จ๋œ router๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” /routes/shop.js ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ–ˆ๋‹ค.

/routes/user.js

const router = require('express').Router();

router.get('/shop/shirts', (req, res) => {
  res.send('์…”์ธ ๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.')
});

router.get('/shop/pants', (req, res) => {
  res.send('๋ฐ”์ง€๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.')
});

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
/server.js

app.use('/', require('./routes/shop.js'));
1
2
3

router ๊ฐ์ฒด๋Š” require('express').Router()๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ๋ถ„๋ฆฌ ์ „ ๊ธฐ์กด ์ฝ”๋“œ์—์„œ๋Š” app.get() / app.post()์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ๋ถ„๋ฆฌ๋œ ํŒŒ์ผ์—์„œ๋Š” router ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์ž‘์„ฑํ•œ๋‹ค.

router์—์„œ๋„ app ๊ณผ ๊ฐ™์ด use, get, post, put, patch, delete ์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ์„ค๋ช…)

์ฒ˜์Œ์— require ๋ฌธ์„ ํ†ตํ•ด Router ์‚ฌ์šฉ์„ ์•Œ๋ ธ๋‹ค๋ฉด, ๋งˆ์ง€๋ง‰์—๋Š” module.exports = router๋กœ ๋ผ์šฐํ„ฐ ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด ๋‹ค๋ฅธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์—์„œ๋„ router ๋ณ€์ˆ˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  server.js ์—์„œ app.use๋กœ ํ•ด๋‹น ๋ผ์šฐํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•ด ํ›จ์”ฌ ๊น”๋”ํ•˜๊ฒŒ ๊ด€๋ฆฌํ–ˆ๋‹ค.

# ๋” ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ

/routes/shop.js

const router = require('express').Router();

router.get('/shirts', (req, res) => {
  res.send('์…”์ธ ๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.')
});

router.get('/pants', (req, res) => {
  res.send('๋ฐ”์ง€๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.')
});

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
/server.js

app.use('/shop', require('./routes/shop.js'));
1
2
3

๊ณตํ†ต๋œ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  server.js ์—์„œ ๊ฒฝ๋กœ๋ฅผ /shop์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์ด๋Š” /shop/shirts, /shop/pants ๋กœ ์—ฐ๊ฒฐ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ์–ด๋””์— ํ•ด๋‹นํ•˜๋Š” ๋ผ์šฐํ„ฐ์ธ์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ง๊ด€์ ์ธ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค.

# router์— ๋ฏธ๋“ค์›จ์–ด ์ ์šฉํ•˜๊ธฐ

/routes/shop.js

const router = require('express').Router();

// ๋ฏธ๋“ค์›จ์–ด ์ƒ์„ฑ
function middleWare() {
  ...
}

router.get('/shirts', middleWare, (req, res) => {
  res.send('์…”์ธ ๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
});

router.get('/pants', middleWare, (req, res) => {
  res.send('๋ฐ”์ง€๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
});

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

app ์—์„œ ์‚ฌ์šฉํ•˜๋“ฏ ๊ฒฝ๋กœ์™€ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์‚ฌ์ด์— ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ๋จผ์ € ์‹คํ–‰๋œ ํ›„ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฏธ๋“ค์›จ์–ด๋ฅผ ๋„ฃ์„์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

# ๊ทธ๋Ÿฐ๋ฐ ๋ชจ๋“  ๋ผ์šฐํ„ฐ์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

/routes/shop.js

const router = require('express').Router();

// ๋ฏธ๋“ค์›จ์–ด ์ƒ์„ฑ
function middleWare() {
  ...
}

router.use(middleWare);

router.get('/shirts', (req, res) => {
  res.send('์…”์ธ ๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
});

router.get('/pants', (req, res) => {
  res.send('๋ฐ”์ง€๋ฅผ ํŒŒ๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.');
});

module.exports = router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ router.use ๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๋‹น ํŒŒ์ผ ๋‚ด ์ „์—ญ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

# ํŠน์ • ๋ผ์šฐํ„ฐ์—๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

๋˜๋Š” ์ด์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ํŠน์ • url ์—๋งŒ ์ ์šฉํ•˜๋Š” ๋ฏธ๋“ค์›จ์–ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” /shirts ๊ฒฝ๋กœ์—๋งŒ ์ ์šฉ๋œ ์ฝ”๋“œ์ด๋‹ค.

router.use('/shirts', middleWare);
1
Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39