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