Node.js - 使用 Express 获取原始请求正文 [英] Node.js - get raw request body using Express

查看:33
本文介绍了Node.js - 使用 Express 获取原始请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Express 时,我的代码是:

When I use Express, and my code is:

app.use(express.bodyParser());

我如何获得原始请求正文?

推荐答案

Edit 2: 1.15.2 版正文解析器模块引入了 原始模式,它以 缓冲区.默认情况下,它还自动处理 deflate 和 gzip 解压缩.示例用法:

Edit 2: Release 1.15.2 of the body parser module introduces raw mode, which returns the body as a Buffer. By default, it also automatically handles deflate and gzip decompression. Example usage:

var bodyParser = require('body-parser');
app.use(bodyParser.raw(options));

app.get(path, function(req, res) {
  // req.body is a Buffer object
});

默认情况下,options 对象具有以下默认选项:

By default, the options object has the following default options:

var options = {
  inflate: true,
  limit: '100kb',
  type: 'application/octet-stream'
};

如果您希望原始解析器解析除 application/octet-stream 之外的其他 MIME 类型,您需要在此处更改它.它还支持通配符匹配,例如 */**/application.

If you want your raw parser to parse other MIME types other than application/octet-stream, you will need to change it here. It will also support wildcard matching such as */* or */application.

注意:以下答案适用于 Express 4 之前的版本,其中中间件仍与框架捆绑在一起.现代等效项是 body-parser 模块,必须单独安装.

Note: The following answer is for versions before Express 4, where middleware was still bundled with the framework. The modern equivalent is the body-parser module, which must be installed separately.

Express 中的 rawBody 属性曾经可用,但自 1.5.1 版后被移除.要获取原始请求正文,您必须在使用 bodyParser 之前放入一些中间件.您还可以在此处阅读有关它的 GitHub 讨论.

The rawBody property in Express was once available, but removed since version 1.5.1. To get the raw request body, you have to put in some middleware before using the bodyParser. You can also read a GitHub discussion about it here.

app.use(function(req, res, next) {
  req.rawBody = '';
  req.setEncoding('utf8');

  req.on('data', function(chunk) { 
    req.rawBody += chunk;
  });

  req.on('end', function() {
    next();
  });
});
app.use(express.bodyParser());

该中间件将从实际数据流中读取,并将其存储在请求的 rawBody 属性中.然后,您可以像这样访问原始正文:

That middleware will read from the actual data stream, and store it in the rawBody property of the request. You can then access the raw body like this:

app.post('/', function(req, res) {
  // do something with req.rawBody
  // use req.body for the parsed body
});

这个方法和bodyParser似乎拒绝共存,因为一个会先消耗另一个请求流,导致第二个永远不会触发end,因此永远不会调用 next() 并挂起您的应用程序.

It seems that this method and bodyParser refuse to coexist, because one will consume the request stream before the other, leading to whichever one is second to never fire end, thus never calling next(), and hanging your application.

最简单的解决方案很可能是修改 bodyParser 的源代码,您可以在 第 57 行 Connect 的 JSON 解析器.这是修改后的版本.

The simplest solution would most likely be to modify the source of bodyParser, which you would find on line 57 of Connect's JSON parser. This is what the modified version would look like.

var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ buf += chunk });
req.on('end', function() {
  req.rawBody = buf;
  var first = buf.trim()[0];
  ...
});

您会在以下位置找到该文件:

You would find the file at this location:

/node_modules/express/node_modules/connect/lib/middleware/json.js.

这篇关于Node.js - 使用 Express 获取原始请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆