源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

node.js中express中间件body-parser的介绍与用法详解

  • 时间:2020-02-05 05:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:node.js中express中间件body-parser的介绍与用法详解
[b]前言[/b] Node中的核心模块分两类:一类是自带的核心模块,如http、tcp等,第二类是第三方核心模块,express就是与http对应的第三方核心模块,用于处理http请求。express在3.0版本中自带有很多中间件,但是在express 4.0以后,就将除static(静态文件处理)以外的其他中间件分离出来了;在4.0以后需要使用中间件时,就需要单独安装好相应的中间件以后调用,以下3.0与4.0中间件的中间件区别(3.0是内置中间件属性名,4.0是需要安装的中间件名称):

Express 3.0 Name

Express 4.0 Name
bodyParser [url=https://github.com/expressjs/body-parser]body-parser[/url]
compress [url=https://github.com/expressjs/compression]compression[/url]
cookieSession [url=https://github.com/expressjs/cookie-session]cookie-session[/url]
logger [url=https://github.com/expressjs/morgan]morgan[/url]
cookieParser [url=https://github.com/expressjs/cookie-parser]cookie-parser[/url]
session [url=https://github.com/expressjs/session]express-session[/url]
favicon [url=https://github.com/expressjs/favicon]static-favicon[/url]
response-time [url=https://github.com/expressjs/response-time]response-time[/url]
error-handler [url=https://github.com/expressjs/errorhandler]errorhandler[/url]
method-override [url=https://github.com/expressjs/method-override]method-override[/url]
timeout [url=https://github.com/expressjs/timeout]connect-timeout[/url]
vhost [url=https://github.com/expressjs/vhost]vhost[/url]
csrf [url=https://github.com/expressjs/csurf]csurf[/url]

[b]body-parser[/b] 我是在学习nodejs时候,对着书本的例子时,使用bodyParser这个中间件,在终端运行出问题,报错大概意思也是express4.0中没有bodyParser这个内置中间件了,还给了body-parser的GitHub源代码地址:[url=https://github.com/expressjs/body-parser]https://github.com/expressjs/body-parser[/url]. [b]经过看源代码下面的说明知道了body-parser的三种用法:[/b] 在讲用法之间,我们需要弄清楚下面四个不同的处理方法:这四个处理方法分别对body的内容采用不同的处理方法;分别是处理json数据、Buffer流数据、文本数据、UTF-8的编码的数据。 [code]bodyParser.json(options)[/code] 、[code]bodyParser.raw(options) [/code]、[code]bodyParser.text(options) [/code]、[code]bodyParser.urlencoded(options)[/code] [b]以下是它的三种用法:[/b] 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方法调用body-parser实例;且use方法没有设置路由路径;这样的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)方法调用body-parser实例;且该方法有设置路由路径;这样的body-parser实例就会对该post(或者get)的请求进行拦截和解析。 3、设置Content-Type 属性;用于修改和设定中间件解析的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' }));
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部