
1. Middleware๋?
1) HTPP ์์ฒญ์์ ์์ํด, ์ต์ข ์๋ต์ผ๋ก ๋ผ์ฐํ ํ๊ธฐ ์ ์ ์ค๊ฐ ๋จ๊ณ๋ฅผ ๋งํ๋ค
2) HTTP ์์ฒญ์ด ๋ค์ด์จ ์๊ฐ ์์๋๋ค.
3) HTTP ์์ฒญ๊ณผ ์๋ต ์ฌ์ด์ ๋จ๊ณ๋ณ ๋์์ ์ํํด์ฃผ๋ ํจ์๋ค.
4) HTTP ์์ฒญ ๊ฐ์ฒด๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋, ์๋ต ๊ฐ์ฒด๋ฅผ ์ฒ๋ฆฌ, ๋ค์ ๋ฏธ๋ค์จ์ด๋ฅผ ์คํํ ์ ์๋ค.
5) HTTP ์๋ต์ด ์ต์ข ์ ์ผ๋ก ๋ง๋ฌด๋ฆฌ๋ ๋๊น์ง, ๋ฏธ๋ค์จ์ด ๋์ ์ฌ์ดํด์ด ์คํ๋๋ค.
6) ์ต์ข ์๋ต์ผ๋ก ๊ฐ์ง ๋ชปํ๊ฒ ์ฐ๊ฒฐ์ ๋์ ์๋ ์๋ค.
๋ฏธ๋ค์จ์ด ์ ์ธํ๊ธฐ
const middleware = (req, res, next) => {
next();
}
req, res, next๋ฅผ ๊ฐ์ง ํจ์๋ฅผ ์์ฑํ๋ฉด ํด๋น ํจ์๋ ๋ฏธ๋ค์จ์ด๋ก ๋์ํ ์ ์๋ค.
- req: HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด, HTTP ์์ฒญ ์ ๋ณด๊ฐ ๋ค์์
- res: HTTP ์๋ต์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด, ์์ฒญ์ ๋ํ ์๋ต์ ๋ฐํํ๋ ๊ฐ์ฒด
- next(): ๋ค์ ๋ฏธ๋ค์จ์ด๋ฅผ ์คํํ๋ ํจ์
2. ๋ฏธ๋ค์จ์ด ์์ฑ๋ฒ
1. ๋ฏธ๋ค์จ์ด๊ฐ ์์ ๊ฒฝ์ฐ
response๊ฐ ์ฆ๊ฐ ๋ฐ์ํ๋ค.
app.get("/", (req, res) => res.send("HomePage Accessed"));
2. ๋ฏธ๋ค์จ์ด๋ฅผ ์ค์ ํ ๊ฒฝ์ฐ
3๊ฐ์ ์ธ์(req, res, next)๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
const middlewareHome = (req, res, next) => {
console.log("๋ฏธ๋ค์จ์ด ์๋ ์๋ฃ");
next();
// next()๋ฉ์๋๊ฐ ์๋ค๋ฉด ๋ฏธ๋ค์จ์ด ์ฌ์ดํด์ด ๋ฉ์ถฐ, ํ๋ฉด์ ๊ณ์ loading ์ํ๊ฐ ๋๋ค.
}
app.get("/", middlewareHome,
(req, res) => res.send("HomePage Accessed"));
// ์คํ ์์
// 1. ๋ฏธ๋ค์จ์ด ์๋ ์๋ฃ
// 2. HomePage Accessed
3. ๋ชจ๋ ํ์ด์ง๊ฐ ์ฌ์ฉํ๋ ๋ฏธ๋ค์จ์ด๋ฅผ ์์ฑํ ๊ฒฝ์ฐ
๋ฏธ๋ค์จ์ด๋ ๋ผ์ฐํธ ์ด์ ์ ์์ฑํด์ผ ์๋ํ๋ค.
๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋์ธ app.use()๊ฐ app.get()๋ณด๋ค ์ ํ๋์ด์ผ ํ๋ค.
const globalMiddleware = (req, res, next) => {
if (!isAdmin(req)) {
next(new Error('Not Authorized'));
return;
}
next();
}
app.use(auth);
app.get('/', responseHome);
app.get('/profile', responseProfile);
// ๋ฃจํธ ํ์ด์ง, profile ํ์ด์ง์ ๊ฐ๊ฐ ๋ผ์ฐํธ ํ ๋ globalMiddleware๊ฐ ์๋ํ๋ค.
4. ๋ฏธ๋ค์จ์ด ์๋ธ ์คํ
use ๋ http method ํจ์์ ์ฌ๋ฌ ๊ฐ์ ๋ฏธ๋ค์จ์ด๋ฅผ ๋์์ ์ ์ฉํ ์ ์๋ค. ์ฃผ๋ก ํ ๊ฐ์ ๊ฒฝ๋ก์ ํน์ ํด์ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค. ์ ๋ฌ๋ ์ธ์์ ์์ ์์ผ๋ก ๋์ํ๋ค.
app.use(middleware1, middlware2, ...);
app.use('/admin', auth, adminRouter);
app.get('/', logger, (req, res, next) => {
res.send('Hello Express');
})
3. ๋ฏธ๋ค์จ์ด ์ฌ์ฉํ๊ธฐ
middleware ๋ ์ ์ฉ๋๋ ์์น์ ๋ฐ๋ผ์ ์ดํ๋ฆฌ์ผ์ด์ ๋ฏธ๋ค์จ์ด, ๋ผ์ฐํฐ ๋ฏธ๋ค์จ์ด, ์ค๋ฅ์ฒ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ก ๋ถ๋ฅํ ์ ์๋ค. ํ์ํ ๋์ ๋ฐฉ์์ ๋ฐ๋ผ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉํ ์์น๋ฅผ ๊ฒฐ์ ํ์.
1) application middleware
use ๋ http method ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฏธ๋ค์จ์ด๋ฅผ ์ฐ๊ฒฐํ ์ ์๋ค. ๋ฏธ๋ค์จ์ด๋ฅผ ๋ชจ๋ ์์ฒญ์ ๊ณตํต์ ์ผ๋ก ์ ์ฉํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก, HTTP ์์ฒญ์ด ๋ค์ด์จ ์๊ฐ๋ถํฐ ์ ์ฉ๋ ์์๋๋ก ๋์ํ๋ค.
app.use((req, res, next) => {
console.log(`Request ${req.path}`);
next();
});
app.use(auth);
app.get('/', (req, res, next) => {
res.send('Hello Express');
});
2) router middleware
router ๊ฐ์ฒด์ ๋ฏธ๋ค์จ์ด๊ฐ ์ ์ฉ๋๋ ๊ฒ ์ด์ธ์๋ ์ดํ๋ฆฌ์ผ์ด์ ๋ฏธ๋ค์จ์ด์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋์ผํ๋ค. ํน์ ๊ฒฝ๋ก์ ๋ผ์ฐํ ์๋ง ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ด๋ค. app ๊ฐ์ฒด์ ๋ผ์ฐํฐ๊ฐ ์ ์ฉ๋ ์ดํ๋ก ์์๋๋ก ๋์ํ๋ค.
router.use(auth); // 3
router.get('/', (req, res, next) => { // 4
res.send('Hello Router');
});
app.use((req, res, next) => { // 1
console.log(`Request ${req.path}`);
next();
});
app.use('/admin', router); // 2
3) error handleing middleware
์ค๋ฅ์ฒ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ฅ ๋ง์ง๋ง์ ์์นํ๋ค. ๋ค๋ฅธ ๋ฏธ๋ค์จ์ด๋ค๊ณผ๋ ๋ฌ๋ฆฌ err, req, res, next ๋ค ๊ฐ์ง ์ธ์๋ฅผ ๊ฐ์ง๋ฉฐ, ์์ ๋ฏธ๋ค์จ์ด์์ next ํจ์์ ์ธ์๊ฐ ์ ๋ฌ๋๋ฉด, ์ค๊ฐ ๋ฏธ๋ค์จ์ด๋ค์ ๋ฐ์ด๋๊ณ ์ค๋ฅ์ฒ๋ฆฌ ๋ฏธ๋ค์จ์ด๊ฐ ๋ฐ๋ก ์คํ๋๋ค.
app.use((req, res, next) => { // 1
if (!isAdmin(req)) {
next(new Error('Not Authorized'));
return;
}
next();
});
app.get('/', (req, res, next) => { // ๊ฑด๋๋
res.send('Hello Express');
});
app.use((err, req, res, next) => { // 2
res.send('Error Occurred');
});
๊ฐ์ฅ ๋ง์ง๋ง ๋ฏธ๋ค์จ์ด๋ก ์ค๋ฅ ์ฒ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉํ๋ฉด, ๋ชจ๋ ๋ผ์ฐํ ์ ๊ณตํต์ ์ธ ์ค๋ฅ์ฒ๋ฆฌ ๋ก์ง์ ์ ์ฉํ ์ ์๋ค.
app.use((err, req, res, next) => {
res.status(500);
res.json({
result: 'fail',
error: err.message,
});
});
์ ์๋์ง ์์ ๋ผ์ฐํ ์ 404 ์ค๋ฅ ์ฒ๋ฆฌํ๊ธฐ
๋ชจ๋ ๋ผ์ฐํ ์ด ์ ์ฉ๋ ์ดํ์ ์ฌ์ฉ๋๋ ๋ฏธ๋ค์จ์ด๋ ์ค์ ๋ ๊ฒฝ๋ก๊ฐ ์๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ Route Handler๋ก ๋์ํ๋ค. Express.js ๋ ๊ธฐ๋ณธ์ ์ธ 404 ํ์ด์ง๋ฅผ ๊ฐ์ง๊ณ ์์ง๋ง, ์ง์ ์ฒ๋ฆฌ๊ฐ ํ์ ํ ๊ฒฝ์ฐ ์ด์ ๊ฐ์ Route Handler๋ฅผ ์ถ๊ฐํด์ผํ๋ค.
app.use((req, res, next) => {
res.status(404);
res.send({
result: 'fail',
error: `Page not found ${req.path}`
});
});
4) ํจ์ํ ๋ฏธ๋ค์จ์ด
ํ๋์ ๋ฏธ๋ค์จ์ด๋ฅผ ์์ฑํ๊ณ , ์๋ ๋ชจ๋๋ฅผ ์ ํํ๋ฉด์ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ, API๋ณ๋ก ์ฌ์ฉ์์ ๊ถํ์ ๋ค๋ฅด๊ฒ ์ ํํ๊ณ ์ถ์ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์๋ค. ํจ์ ์คํ ์ ๋ฏธ๋ค์จ์ด์ ๋์์ด ๊ฒฐ์ ๋๋ ๋ฐฉ์์ผ๋ก, ๋์ผํ ๋ก์ง์ ์ค์ ๊ฐ๋ง ๋ค๋ฅด๊ฒ ๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ํ์ฉ๋๋ค.
const auth = (memberType) => {
return (req, res, next) => {
if (!checkMember(req, memberType)) {
next(new Error(`member not ${memberType}`));
return;
}
next();
}
}
app.use('/admin', auth('admin'), adminRouter);
app.use('/users', auth('member'), userRouter);'๐ Back > ๐งฉ Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Express ๋ฏธ๋ค์จ์ด ์ฝ๋] app.use(), app.get(), Router() (0) | 2021.01.25 |
|---|---|
| [Express ๋ฏธ๋ค์จ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ] morgan, helmet, cookie-parser, body-parser (0) | 2021.01.25 |
| [Express ์ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ]: babel, nodemon (0) | 2021.01.21 |
| [Express ์ ๋ฌธ] ์น ์๋ฒ ํ๋ ์ ์ํฌ express๋? (0) | 2021.01.20 |
| [Node] ๋ธ๋ผ์ฐ์ ๋ฐ์ JavaScript (0) | 2021.01.20 |