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

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

问题描述

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

When I use Express, and my code is:

app.use(express.bodyParser());

如何获取原始请求正文

推荐答案

编辑2:发布身体解析器模块的1.15.2引入原始模式,它作为缓冲区。默认情况下,它也自动处理放气和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
});

默认情况下,选项以下默认选项:

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

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

如果您希望您的原始解析器解析除应用程序/八位字节流,您需要在此处更改。它还将支持通配符匹配,例如 * / * * / 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之前的版本,其中中间件仍然与框架捆绑在一起。现代相当的是正文解析器模块,必须单独安装。

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讨论 here

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行。这是修改版本的样子。

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天全站免登陆