티스토리 뷰
반응형
http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4
express/connect top-level generic
This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})express route-specific
This example demonstrates adding body parsers specifically to the routes that need them. In general, this is the most recommend way to use body-parser with express.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
res.send('welcome, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
// create user in req.body
})change content-type for parsers
All the parsers accept a type option which allows you to change the Content-Type that the middleware will parse.
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))반응형
'웹프로그래밍 > node.js & Typescript' 카테고리의 다른 글
| [node.js] Most middleware (like logger) is no longer bundled with Express and must be installed separately... (0) | 2015.06.09 |
|---|---|
| [node.js] express cookie-parser (0) | 2015.06.09 |
| [node.js] Check synchronously if file/directory exists in Node.js (0) | 2015.06.09 |
| [node.js] file upload (0) | 2015.06.09 |
| [node.js] .router' is deprecated! (0) | 2015.06.09 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 연말정산
- 지도
- 해외주식
- 주담대
- 재테크
- 고금리
- 카카오뱅크
- 세금
- 2026
- 공제
- 우회
- 아파트매매
- 안과
- 안드로이드
- etf
- 신혼부부
- 금융
- 앱테크
- 대출
- ubuntu
- 투자
- php
- 카뱅
- 부동산
- 혜택
- 적금
- 내집마련
- 출산
- remove
- 정부
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
| 30 | 31 |
글 보관함