body-parser
말그대로 body를 parsing해주는 미들웨어.
들어온 데이터를 원하는 형태로 변환해준다고 생각하면 됨
body-parser 없이도 post로 보낸 값을 받아올 수 있지만, body-parser를 사용함으로써 더 간단한 코드를 작성할 수 있게된다.
install한 body-parser를 장착
var bodyParser = require('body-parser')
그리고
app.use(bodyParser.urlencoded({ extended: false }))
위의 코드 한 줄이면 request.body에 접근해 데이터를 가져올 수 있게 됩니다.
간단히 사용해보았음
이게 POST 방식일 때만 해당되는 것 인가..?
아마 GET 방식의 값을 받을 땐 req.query를 사용하면 될 것이다
+) 공식 document 및 여러 참고할만한 자료들
Using Express middleware
Using middleware Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls. Middleware functions are functions that have access to the request ob
expressjs.com
expressjs/body-parser
Node.js body parsing middleware. Contribute to expressjs/body-parser development by creating an account on GitHub.
github.com
What does body-parser do with express?
I don't understand why we need body-parser in an Express application, as we can get data without using body-parser. And what does it do actually and how?
stackoverflow.com
'STUDY > Node.js' 카테고리의 다른 글
Node.js | Express application generator (0) | 2019.12.23 |
---|---|
Node.js | Express Router (라우터 분리하여 관리하기) (0) | 2019.12.23 |
Node.js | PM2 사용하기 (0) | 2019.12.19 |
Node.js | Express 사용하기 (기본 Routing) (0) | 2019.12.18 |
Node.js | 파일 시스템(FileSystem) (0) | 2019.12.17 |