body-parser에 대해 알아보자.
body-parser
HTTP 의 post, put 요청시 HTTP 의 본문(body)를 parsing 하여 나온 결과값을 req.body
에 넣어 body 프로퍼티를 사용할 수 있도록 합니다.
아래의 테스트 코드에서 req.body
를 콘솔로그로 출력해보면 undefined가 출력합니다.
즉, Express는 요청을 처리할 때 기본적으로 body를 undefined로 처리하고 있습니다.
1
2
3
4
app.get("/test", (req, res) => {
res.send("test");
console.log(req.body);
});
body-parser 모듈로 미들웨어 함수를 등록하면, req의 body부분을 자신이 원하는 형태로 파싱하여 활용할 수 있습니다.
body-parser의 parser
- JSON body parser
- URL-encoded from body parser
- Raw body parser
- Text body Parser
위 4가지 parser 중에서 가장 많이 사용되는 JSON body parser 와 URL-encoded from body parser 에 대해 알아보겠습니다.
JSON body parser
HTTP 의 본문(body)를 JSON 으로 파싱합니다.
HTTP 의 헤더(header) Content-Type 속성 값이 “application/json” 이 아닐 경우에는 파싱하지 않습니다.
옵션
inflate (default : true)
압축된 요청 body의 처리를 허용합 지 설정합니다.`*limit** (default : 100kB)
최대 요청 body의 크기를 제한합니다.reviver (default : null)
JSON.parse() 의 두 번째 인자로 직접 전달됩니다.
reviver가 주어지면 분석한 값을 반환하기 전에 변환합니다.1 2 3 4
JSON.parse('{"p": 5}', (key, value) => typeof value === "number" ? value * 2 : value ); // { p: 10 }
strict (default : true)
true로 설정 시 배열과 객체만 파싱
false로 설정하면 JSON.parse()가 허용하는 모든 항목 허용
(즉, 숫자, 문자열 등의 원시 타입 데이터도 파싱해준다.)type (default : application/json)
미들웨어가 파싱할 미디어의 타입을 결정하는 옵션
URL-encoded from body parser
HTTP 의 본문(body)를 x-www-form-urlencoded 으로 파싱합니다.
HTTP 의 헤더(header) Content-Type 속성 값이 “x-www-form-urlencoded” 이 아닐 경우에는 파싱하지 않습니다.
옵션
extended
false로 설정 시 Node.js에 기본으로 내장된 queryString 모듈을 사용합니다.
true로 설정 시 qs 모듈을 사용합니다.queryString 모듈 : url 주소 뒤에 붙어서 넘어오는 파라미터인 querystring을 쉽게 조작할 수 있는 기능을 제공하는 모듈
qs 모듈 : 별도 설치가 필요한 queryString 모듈의 기능보다 확장된 모듈
body-parser 설정
application/json 은 {key: value}
형태이고, application/x-www-form-urlencoded는 key=value&key=value
의 형태입니다.
즉, JSON 형식으로 { name: ‘psmin’, book: ‘nodejs’ } 를 본문으로 보낸다면 req.body에 그대로 들어가며 URL-encoded 형식으로 name=psmin&book=nodejs를 본문으로 보낸다면 req.body에 { name: ‘psmin’, book: ‘nodejs’ } 가 들어갑니다.
Express 4.16.0 버전 이상부터 내장모듈로 포함됐기 때문에 별도로 설치할 필요 없이 간단한 설정을 통해 바로 사용할 수 있습니다.
1
2
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
예제 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import express from "express";
import bodyParser from "body-parser";
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// route
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/test", (req, res) => {
console.log(req.body);
var msg = `name : ${req.body.name}\nage : ${req.body.age}`;
res.send(msg);
});
app.post("/test", (req, res) => {
console.log(req.body);
var msg = `name : ${req.body.name}\nage : ${req.body.age}`;
res.send(msg);
});
// 3000 포트로 서버 오픈
app.listen(3000, () => {
console.log("express server on port 3000");
});
Postman으로 테스트 해보기
Postman를 이용하여 HTTP 본문(body) 에 name 과 age 프로퍼티를 갖는 JSON 객체를 보내보겠습니다.
여기서 주의할 점은 HTTP 본문(body) 의 type 이 JSON 이라는 것을 설정해줘야 합니다.
req.body.name 과 req.body.age가 잘 확인됩니다.
다음은 HTTP 본문(body) 에는 name 과 age key를 갖는 form 데이터를 넣어보겠습니다.
여기서도 주의할 점은 보내고자하는 HTTP 본문(body) 의 type 이 “x-www-form-urlencoded” 이라는 것을 설정해줘야 합니다.
잘 접근되는 것을 확인 할 수 있습니다.