Skip to content

Latest commit

ย 

History

History
134 lines (100 loc) ยท 3.21 KB

Middleware.md

File metadata and controls

134 lines (100 loc) ยท 3.21 KB

Middleware

1. ๋ฏธ๋“ค์›จ์–ด๋ž€?

  • ์ž๋™์ฐจ ๊ณต์žฅ์˜ ๊ณต์ •๊ณผ ๋น„์Šท, ์š”์ฒญ์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๋”ํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ œ๊ฐ€ ๋ฐœ๊ฒฌ๋œ ๊ฒƒ์„ ๋ฐ–์œผ๋กœ ๊ฑท์–ด๋‚ด๋Š” ์—ญํ• 
  • Express์˜ ํฐ ์žฅ์ 
  • Node.js๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•œ ์„œ๋ฒ„์—์„œ๋Š” ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์„ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ ์šฉ ๊ฐ€๋Šฅ

2. ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ƒํ™ฉ

(1) POST ์š”์ฒญ ๋“ฑ์— ํฌํ•จ๋œ body(payload)๋ฅผ ๊ตฌ์กฐํ™”ํ•  ๋•Œ (์‰ฝ๊ฒŒ ์–ป์–ด๋‚ด๊ณ ์ž ํ•  ๋•Œ)

Node.js๋กœ HTTP ์š”์ฒญ body๋ฅผ ๋ฐ›๋Š” ์ฝ”๋“œ

let body = [];
request.on('data', (chunk) => {
  body.push(chunk);
}).on('end',() => {
  body = Buffer.concat(body).toString();
  // body ๋ณ€์ˆ˜์—๋Š” ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ payload๊ฐ€ ๋‹ด๊น€
  // ๋„คํŠธ์›Œํฌ ์ƒ์˜ chunk๋ฅผ ํ•ฉ์น˜๊ณ , buffer๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
})

body-parser ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
// ... ์ƒ๋žต
app.post('/users', jsonParser, function(req, res) {

})

Express v4.16.0๋ถ€ํ„ฐ๋Š” body-parser๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๋‚ด์žฅ ๋ฏธ๋“ค์›จ์–ด์ธ express.json() ์‚ฌ์šฉ

const jsonParser = express.json();
// ... ์ƒ๋žต
app.post('/api/users', jsonParser, function(req, res) {

})

express.json() ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ์— ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋ฉด? โ†’ options์— {strict: false}๋ฅผ ์ถ”๊ฐ€

const jsonParser = express.json({strict: false});
// ... ์ƒ๋žต
app.post('/api/users', jsonParser, function(req, res){

})

(2) ๋ชจ๋“  ์š”์ฒญ/์‘๋‹ต์— CORS ํ—ค๋”๋ฅผ ๋ถ™์—ฌ์•ผ ํ•  ๋•Œ

Node.js์— CORS๋ฅผ ์ ์šฉํ•˜๊ธฐ

// ๋ผ์šฐํŒ…๋งˆ๋‹ค ํ—ค๋”๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•จ
const defaultCorsHeader = { 
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
  'Access-Control-Allow-Headers': 'Content-Type, Accept',
  'Access-Control-Max-Age': 10
};
// ... ์ƒ๋žต

// OPTIONS ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ๋ผ์šฐํŒ…๋„ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
if (req.method === 'OPTIONS') {
  res.writeHead(200, deaultCorsHeader);
  res.end()
}

cors ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ: ๋ชจ๋“  ์š”์ฒญ์— CORS ํ—ˆ์šฉ

cons cors = require('cors');
// ... ์ƒ๋žต
app.use(cors());

cors ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ: ํŠน์ • ์š”์ฒญ์— CORS ํ—ˆ์šฉ

const cors = require('cors')
// ... ์ƒ๋žต
app.get('/products/:id', cors(), function(req, res, next) {
  res.json({msg: 'This is CORS-enabled for a Single Route'})
})

(3) ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด url์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™•์ธํ•  ๋•Œ

use ๋ฉ”์†Œ๋“œ๋กœ ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด ๋ฏธ๋“ค์›จ์–ด ์ ์šฉ

const express = require('express');
const app = express();

const myLogger = function (req, res, next) {
  console.log('LOGGED');
  next();
};

app.use(myLogger);

app.get('/', function(req, res) {
  res.send('Hello World');
});

app.listen(3000);

(4) ์š”์ฒญ ํ—ค๋”์— ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ

HTTP ์š”์ฒญ์— ํ† ํฐ์ด ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜์—ฌ, ์ด๋ฏธ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์ผ ๊ฒฝ์šฐ ์„ฑ๊ณต, ์•„๋‹ ๊ฒฝ์šฐ ์—๋Ÿฌ

app.use((req, res, next) => {
  // ํ† ํฐ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
  if(req.headers.token) {
    req.isLoggedIn = true;
    next();
  } else {
    res.status(400).send('invalid users')
  }
})