참고자료
- https://velog.io/@hyunju-song/body-parser의-urlencoded는-도대체-어떤-역할을-하는-걸까
- https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0/45690436#45690436
- https://expressjs.com/ko
- https://tk-one.github.io/2018/08/28/nodejs-buffer/
body-parser 모듈
개념
http 통신 과정에서 요청된 request 객체를 추출하는 모듈이다. JS는 데이터를 주고 받을 때 JSON 객체 형태로 주고 받는데, 해당 객체에 담긴 데이터를 쉽게 다룰 수 있도록 파싱해주는 역할을 수행한다.
활용
express 4.x 이후로 express는 body-parser 모듈의 일부 기능을 내장하고 있다. express가 데이터를 주고 받는 방식을 설정하는 아래의 property들은 모두 body-parser를 활용하여 데이터를 파싱하고 있다.
import express from 'express';
express.json() // 송신된 데이터가 json 객체일 경우만 파싱하여 body 객체에 담음
express.raw() // 송신된 데이터가 buffer 객체일 경우만 파싱하여 body 객체에 담음
express.text() // 송신된 모든 body를 string로 바꾸어 통신
express.urlencoded() // 송신된 데이터를 body 객체 내부에 key:value 쌍으로 만듦
express.urlencoded()
개념
어떠한 형태로든 파싱된 req.body “객체”를 key: value 쌍으로 바꿔주는 미들웨어
활용
extended option은 boolean 값을 가진다.
extended: false
value 값은 string 또는 array만 가능하며
body-parser에 내장된 query-string 모듈을 이용한다.
extended: true일 경우 value는 any type 가능하다
body-parser가 내장하지 않은 qs 모듈을 이용하므로 별개로 설치해야 한다.
'Web' 카테고리의 다른 글
[node.js] node-schedule, schedulejob 객체(readonly) 수정하기 (0) | 2022.08.30 |
---|---|
[Nest] Internal server error (0) | 2022.08.25 |
[CORS] CSR에서 google oauth2.0 적용기 (0) | 2022.07.29 |
[Express] request 객체에 object 담기 (0) | 2022.07.01 |
[Express] request.body 빈 필드 validate하는 코드 스타일 고민 (0) | 2022.06.28 |