API
請求體的四種解析方式
- bodyParser.json(options): 解析json數據
- bodyParser.raw(options): 解析二進制格式(Buffer流數據)
- bodyParser.text(options): 解析文本數據
- bodyParser.urlencoded(options): 解析UTF-8的編碼的數據。
bodyParser
變量是對中間件的引用。請求體解析後,解析值都會被放到req.body
屬性,內容爲空時是一個{}空對象。
bodyParser.json(options)
返回一個僅解析json格式數據的中間件
options的可選項
- inflate - 設置爲true時,deflate壓縮數據會被解壓縮;設置爲true時,deflate壓縮數據會被拒絕。默認爲true。
- limit - 設置請求的最大數據量。默認爲’100kb’
- reviver - 傳遞給JSON.parse()方法的第二個參數,詳見JSON.parse()
- strict - 設置爲true時,僅會解析Array和Object兩種格式;設置爲false會解析所有JSON.parse支持的格式。默認爲true
- type - 該選項用於設置爲指定MIME類型的數據使用當前解析中間件。這個選項可以是一個函數或是字符串,當是字符串是會使用type-is來查找MIMI類型;當爲函數是,中間件會通過fn(req)來獲取實際值。默認爲application/json。
- verify - 這個選項僅在verify(req, res, buf, encoding)時受支持
bodyParser.raw(options)
返回一個將所有數據做爲Buffer
格式處理的中間件.其後的所有的req.body
中將會是一個Buffer
值。
options的可選項
- inflate - 設置爲true時,deflate壓縮數據會被解壓縮;設置爲true時,deflate壓縮數據會被拒絕。默認爲true。
- limit - 設置請求的最大數據量。默認爲’100kb’
- type - 該選項用於設置爲指定MIME類型的數據使用當前解析中間件。這個選項可以是一個函數或是字符串,當是字符串是會使用type-is來查找MIMI類型;當爲函數是,中間件會通過fn(req)來獲取實際值。默認爲application/octet-stream。
- verify - 這個選項僅在verify(req, res, buf, encoding)時受支持
bodyParser.text(options)
解析文本格式 返回一個僅處理字符串格式處理的中間件。其後的所有的req.body
中將會是一個字符串值。
options選項
- defaultCharset - 如果Content-Type後沒有指定編碼時,使用此編碼。默認爲’utf-8’
- inflate - 設置爲true時,deflate壓縮數據會被解壓縮;設置爲true時,deflate壓縮數據會被拒絕。默認爲true。
- limit - 設置請求的最大數據量。默認爲’100kb’
- type - 該選項用於設置爲指定MIME類型的數據使用當前解析中間件。這個選項可以是一個函數或是字符串,當是字符串是會使用type-is來查找MIMI類型;當爲函數是,中間件會通過fn(req)來獲取實際值。默認爲application/octet-stream。
- verify - 這個選項僅在verify(req, res, buf, encoding)時受支持
bodyParser.urlencoded(options)
解析UTF-8的編碼的數據。返回一個處理urlencoded
數據的中間件。
options選項
- extended - 當設置爲false時,會使用querystring庫解析URL編碼的數據;當設置爲true時,會使用qs庫解析URL編碼的數據。後沒有指定編碼時,使用此編碼。默認爲true
- inflate - 設置爲true時,deflate壓縮數據會被解壓縮;設置爲true時,deflate壓縮數據會被拒絕。默認爲true。
- limit - 設置請求的最大數據量。默認爲’100kb’
- parameterLimit - 用於設置URL編碼值的最大數據。默認爲1000
- type - 該選項用於設置爲指定MIME類型的數據使用當前解析中間件。這個選項可以是一個函數或是字符串,當是字符串是會使用type-is來查找MIMI類型;當爲函數是,中間件會通過fn(req)來獲取實際值。默認爲application/octet-stream。
- verify - 這個選項僅在verify(req, res, buf, encoding)時受支持
使用方式
var express= require('express');
var bodyParser = require('body-parser');
var app=new express();
//創建application/json解析
var jsonParser=bodyParser.json();
//創建application/x-www-form-urlencoded解析
var urlencodeParser = bodyParser.urlencoded({extended:false});
app.use(urlencodeParser);//使用中間件
app.get('/',function(req,res){
res.sendfile(__dirname+'/index.html');
});
app.post('/login',function(req,res){
if(!req.body) return res.sendStatus(400);
res.send('welcome,'+req.body.username);
console.log(req.body);//在終端打印請求體
});
app.listen(3000);
必須指定content-type
Express's bodyParser only parses the incoming data, if the content-type
is set to either of the following:
application/x-www-form-urlencoded
application/json
multipart/form-data
一般使用:
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))
})
Change accepted type for parsers
All the parsers accept a type
option which allows you to change the Content-Type
that the middleware will parse.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// 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' }))