栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何禁用Express BodyParser进行文件上传(Node.js)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何禁用Express BodyParser进行文件上传(Node.js)

当您键入时

app.use(express.bodyParser())
,几乎每个请求都将通过
bodyParser
函数(将执行哪个请求取决于
Content-Type
标头)。

默认情况下,支持3个标头(AFAIR)。您可以确定来源。您可以使用以下方法来(重新)定义的处理程序

Content-Type

var express = require('express');var bodyParser = express.bodyParser;// redefine handler for Content-Type: multipart/form-databodyParser.parse('multipart/form-data') = function(req, options, next) {  // parse request body your way; example of such action:  // https://github.com/senchalabs/connect/blob/master/lib/middleware/multipart.js  // for your needs it will probably be this:  next();}

更新。

Express 3发生了变化,因此我正在共享工作项目中的更新代码(应在 之前

app.use
编辑): __
express.bodyParser()

var connectUtils = require('express/node_modules/connect/lib/utils');module.exports = function(contentTypes) {  contentTypes = Array.isArray(contentTypes) ? contentTypes : [contentTypes];  return function (req, res, next) {    if (req._body)      return next();    req.body = req.body || {};    if (!connectUtils.hasBody(req))      return next();    if (-1 === contentTypes.indexOf(req.header('content-type')))      return next();    req.setEncoding('utf8');  // Reconsider this line!    req._body   = true;       // Mark as parsed for other body parsers.    req.rawBody = '';    req.on('data', function (chunk) {      req.rawBody += chunk;    });    req.on('end', next);  };};

还有一些关于原始问题的伪代码:

function disableParserForContentType(req, res, next) {  if (req.contentType in options.contentTypes) {    req._body = true;    next();  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415624.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号