node.js中express中間件body-parser的介紹與用法詳解
前言
Node中的核心模塊分兩類:一類是自帶的核心模塊,如http、tcp等,第二類是第三方核心模塊,express就是與http對(duì)應(yīng)的第三方核心模塊,用于處理http請(qǐng)求。express在3.0版本中自帶有很多中間件,但是在express 4.0以后,就將除static(靜態(tài)文件處理)以外的其他中間件分離出來(lái)了;在4.0以后需要使用中間件時(shí),就需要單獨(dú)安裝好相應(yīng)的中間件以后調(diào)用,以下3.0與4.0中間件的中間件區(qū)別(3.0是內(nèi)置中間件屬性名,4.0是需要安裝的中間件名稱):
|
Express 3.0 Name |
Express 4.0 Name |
|
bodyParser |
|
|
compress |
|
|
cookieSession |
|
|
logger |
|
|
cookieParser |
|
|
session |
|
|
favicon |
|
|
response-time |
|
|
error-handler |
|
|
method-override |
|
|
timeout |
|
|
vhost |
|
|
csrf |
body-parser
我是在學(xué)習(xí)nodejs時(shí)候,對(duì)著書(shū)本的例子時(shí),使用bodyParser這個(gè)中間件,在終端運(yùn)行出問(wèn)題,報(bào)錯(cuò)大概意思也是express4.0中沒(méi)有bodyParser這個(gè)內(nèi)置中間件了,還給了body-parser的GitHub源代碼地址:https://github.com/expressjs/body-parser.
經(jīng)過(guò)看源代碼下面的說(shuō)明知道了body-parser的三種用法:
在講用法之間,我們需要弄清楚下面四個(gè)不同的處理方法:這四個(gè)處理方法分別對(duì)body的內(nèi)容采用不同的處理方法;分別是處理json數(shù)據(jù)、Buffer流數(shù)據(jù)、文本數(shù)據(jù)、UTF-8的編碼的數(shù)據(jù)。
bodyParser.json(options) 、bodyParser.raw(options) 、bodyParser.text(options) 、bodyParser.urlencoded(options)
以下是它的三種用法:
1、底層中間件用法:這將攔截和解析所有的請(qǐng)求;也即這種用法是全局的。
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的use方法調(diào)用body-parser實(shí)例;且use方法沒(méi)有設(shè)置路由路徑;這樣的body-parser實(shí)例就會(huì)對(duì)該app所有的請(qǐng)求進(jìn)行攔截和解析。
2、特定路由下的中間件用法:這種用法是針對(duì)特定路由下的特定請(qǐng)求的,只有請(qǐng)求該路由時(shí),中間件才會(huì)攔截和解析該請(qǐng)求;也即這種用法是局部的;也是最常用的一個(gè)方式。
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
})
express的post(或者get)方法調(diào)用body-parser實(shí)例;且該方法有設(shè)置路由路徑;這樣的body-parser實(shí)例就會(huì)對(duì)該post(或者get)的請(qǐng)求進(jìn)行攔截和解析。
3、設(shè)置Content-Type 屬性;用于修改和設(shè)定中間件解析的body類容類型。
// 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' }));
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Node.js 使用 Express-Jwt和JsonWebToken 進(jìn)行Token身份
這篇文章主要介紹了Node.js 使用 Express-Jwt和JsonWebToken 進(jìn)行Token身份驗(yàn)證的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Node.js學(xué)習(xí)之TCP/IP數(shù)據(jù)通訊(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Node.js學(xué)習(xí)之TCP/IP數(shù)據(jù)通訊(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
nodejs+express最簡(jiǎn)易的連接數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了nodejs+express 最簡(jiǎn)易的連接數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Mongoose中document與object的區(qū)別示例詳解
這篇文章主要給大家介紹了關(guān)于Mongoose中document與object區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

