2023. 8. 27. 20:17ใ3. Node.js
Express ๋ฏธ๋ค์จ์ด
Express์์ ๋ฏธ๋ค์จ์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ฅ์ ํ์ฅํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด, ๋ก๊น , ์ธ์ฆ, ์ธ์ ๊ด๋ฆฌ, ์ค๋ฅ ์ฒ๋ฆฌ ๋ฑ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์ต๋๋ค. ๊ตฌ์ฑํ๋ ํน์ ๊ธฐ๋ฅ ์คํ์ผ ์ํ๋ ์ฝ๋๋ฅผ ์คํํ ์์ง๋ง ๋๊ฒ๋ ๋ค์ด์ค๋ ์์ฒญ ์ฒ๋ฆฌ, ์๋ต ์ ์ก ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ค.
Express ๊ณต์ ํํ์ด์ง์์ ํ์ธ ๊ฐ๋ฅํ ๋ฏธ๋ค์จ์ด์ ์ ํ
๋ฏธ๋ค์จ์ด๋ ์ ์ฐํ๋ค. Express์๊ฒ ์๋ก ๋ค๋ฅธ ๊ฒฝ๋ก์ ๋ํด ๋์ผํ ๋ฏธ๋ค์จ์ด ๊ธฐ๋ฅ์ ์คํํ๋๋ก ์ง์ํ์ฌ ์๋ก ๋ค๋ฅธ API ์๋ํฌ์ธํธ์์ ๊ณตํต ๊ฒ์ฌ๋ฅผ ์ํํ๋ ๋ฑ์ ์์ ์ ์ํํ ์ ์๋ค.
์์ ๋ง์ ๋ฏธ๋ค์จ์ด ๊ธฐ๋ฅ์ ์์ฑํ๋ ๊ฒ ์ธ์๋ ํ์ฌ ๋ฏธ๋ค์จ์ด๋ฅผ ์ค์นํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค. Express ๋ฌธ์์๋ ์์ฃผ ์ฌ์ฉ์ด๋๋ ๋ชจ๋๋ค์ด ๋์ด๋์ด ์๋ค.
์ต์คํ๋ ์ค ๊ณต์ ํํ์ด์ง(https://expressjs.com/)
Express - Node.js web application framework
Fast, unopinionated, minimalist web framework for Node.js $ npm install express --save
expressjs.com


์๋จ์ ์ํ์ฝ๋ฉ YOUTUBE๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ณต๋ถํ ๋ด์ฉ์ ์๋์ ๋ช ๊ฐ์ง ์ฐธ๊ณ ํ์ฌ ์ถ๊ฐ๋ก ๋ ๊ธฐ์ฌํด ๋ณด์๋ค.
Application-level middleware (๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ Type)
- app์ด๋ผ๋ ๋ณ์๋ Application์ด๋ผ๋ ๊ฐ์ฒด๊ฐ ๋ด๊ฒจ์๋๋ฐ ๊ฐ์ฒด์ get, use, post ๋ฐฉ์์ ํตํด ๋ฏธ๋ค์จ์ด๋ฅผ ๋ฑ๋กํ ์ ์๋ค.
์ด๋ฌํ ๋ฐฉ๋ฒ์ Application-level middleware ๋ผ๊ณ ํ๋ค.
๋ฏธ๋ค์จ์ด ํจ์๋ app.use() ๋๋ app.METHOD() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฑ๋ก
app.use() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ HTTP ์์ฒญ์์ ๋ฏธ๋ค์จ์ด ํจ์๊ฐ ์คํ
app.METHOD() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ HTTP ์์ฒญ์์๋ง ๋ฏธ๋ค์จ์ด ํจ์๊ฐ ์คํ
์๋ฅผ ๋ค๋ฉด, app.get() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด GET ์์ฒญ์์๋ง ๋ฏธ๋ค์จ์ด ํจ์๊ฐ ์คํ
๋ฏธ๋ค์จ์ด ํจ์๋ ๋ค์๊ณผ ๊ฐ์ ์ธ์๋ฅผ ๊ฐ์ง ์ ์๋ค.
req: HTTP ์์ฒญ ๊ฐ์ฒด
res: HTTP ์๋ต ๊ฐ์ฒด
next: ๋ค์ ๋ฏธ๋ค์จ์ด ํจ์๋ก ์ ์ด๋ฅผ ์ ๋ฌํ๋ ํจ์
๋ฏธ๋ค์จ์ด ํจ์๋ ๋ค์ ๋ฏธ๋ค์จ์ด ํจ์๋ก ์ ์ด๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด next() ํจ์๋ฅผ ํธ์ถํด์ผ ํ๋ค.
๊ทธ๋ ์ง ์์ผ๋ฉด ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต์ด ์ ์ก๋์ง ์๋๋ค.
Application-level middleware ๋ช ๊ฐ์ง ์ฌ์ฉ๋ฒ์ ์์๋ณด์!
1) ๊ฒฝ๋ก๊ฐ ์๋ ๋ฏธ๋ค์จ์ด ๊ธฐ๋ฅ์ ๋ณด์ฌ์ค๋ค. ์ด ํจ์๋ ์ฑ์ด ์์ฒญ์ ๋ฐ์ ๋๋ง๋ค ์คํ๋๋ค.
const express = require('express')
const app = express()
app.use((req, res, next) => {
console.log('Time:', Date.now())
next()
})
2) app.use ์ (/user/:id) ๊ฒฝ๋ก๋ฅผ ์ฃผ๋ ๊ฒ์ ํตํด, ํน์ ๊ฒฝ๋ก์๋ง ๋ฏธ๋ค์จ์ด๋ง ๋์๋๊ฒ ํ ์ ์๋ค.
app.use('/user/:id', (req, res, next) => {
console.log('Request Type:', req.method)
next()
})
3. get์ ํตํด์ ๋ฉ์๋๊ฐ get๋ฐฉ์์ผ๋๋ง ๋์ํ๋๋ก ํ ์ ์๋ค.
app.get('/user/:id', (req, res, next) => {
res.send('USER')
})
4. ์ธ์๋ก ํจ์๋ฅผ ์ฐ์์ ์ผ๋ก ์ฃผ๋ ๊ฒ์ ํตํด ๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ๋ฌ ๊ฐ ์ฌ์ฉํ ์ ์๋ค.
ํจ์๊ฐ ์ฒ์์ ์คํ์ด ๋๊ณ , ํจ์ ์์์ next๋ฅผ ํธ์ถํ๋ฉด ์์๋๋ก ์คํ์ด ๊ฐ๋ฅํ๋ค.
app.use('/user/:id', (req, res, next) => {
console.log('Request URL:', req.originalUrl)
next()
}, (req, res, next) => {
console.log('Request Type:', req.method)
next()
})
5. ์กฐ๊ฑด๋ฌธ์ ํตํด์ ๋ค์ ๋ฏธ๋ค์จ์ด ์คํ์ ์ํฌ์ง ์ง์ ์ด ๊ฐ๋ฅํ๋ค.
๋ง์ฝ, req.params.id ๊ฐ์ด '0'์ด๋ผ๋ฉด, next('route')๋ฅผ ์คํํด์ผ ํ๋ค. ์ฆ ๋ค์ route๋ฅผ ์คํํด์ผ ํ๋ฏ๋ก res.send('special')์ ์คํ์ํจ๋ค. ๋ง์ฝ '0'์ด ์๋๋ผ๋ฉด, next()๊ฐ ์คํ๋๋ฉฐ next์ ๊ฐ์ด ์คํ๋๋ค.
app.get('/user/:id', (req, res, next) => {
// if the user ID is 0, skip to the next route
if (req.params.id === '0') next('route')
// otherwise pass the control to the next middleware function in this stack
else next()
}, (req, res, next) => {
// send a regular response
res.send('regular')
})
// handler for the /user/:id path, which sends a special response
app.get('/user/:id', (req, res, next) => {
res.send('special')
})
์ฐธ๊ณ ํ๋ฉด ์ข์ ์ฌ์ดํธ :
https://simonplend.com/how-does-middleware-work-in-express/
How does middleware work in Express?
The middleware pattern is fundamental to building applications with Express, but are you really comfortable with how it works?
simonplend.com

'3. Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [ Node.js ์๋ จ์ฃผ์ฐจ1 (1-1)] ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค (RDB) (3) | 2023.09.01 |
|---|---|
| [ Node.js ์ ๋ฌธ์ฃผ์ฐจ2 (2-0)] ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ๊ฐ๋ฅํ joi (0) | 2023.08.31 |
| [ Node.js ์ ๋ฌธ์ฃผ์ฐจ2 (2-0)] REST API, RESTful (0) | 2023.08.27 |
| [ Node.js ์ ๋ฌธ์ฃผ์ฐจ1 (1-12)] Express.js ํ๋ ์์ํฌ (0) | 2023.08.26 |
| [ Node.js ์ ๋ฌธ์ฃผ์ฐจ1 (1-3)] ์น๊ณผ HTTP์ ๋์ ๋ฐฉ์ (0) | 2023.08.25 |