gpt4 book ai didi

javascript - 使用 Busboy 迁移 Express 应用程序中的 bodyParser()?

转载 作者:行者123 更新时间:2023-11-28 19:17:13 24 4
gpt4 key购买 nike

作为 Nodejs 的新手,我直接开始编写一个简单的应用程序,而没有真正阅读良好的安全实践。我刚刚发现对所有路由使用 bodyParser() 实际上是 a bad thing because it allows for DOS attack using multipart files .

建议的修复方法是仅根据路由加载特定模块。即,对于分段文件上传,请使用multipart。对于没有文件上传的常规 POST(即文本表单提交),请使用 express.json()、express.urlencoded()

或者另一种选择是使用 busboyconnect-busboy 。但我感到困惑的是如何指定哪个路由应该处理多部分数据,哪个不应该?否则,我不会遇到与 bodyParser 相同的问题吗?

此外,busboy 文档说它不处理 GET:

If you find that req.busboy is not defined in your code when you expect it to be, check that the following conditions are met. If they are not, req.busboy won't be defined:
1. The request method is not GET or HEAD

所以,我更困惑如何解析 GET 中的 params。我认为 bodyParser 为我做了这个,这样我就可以使用 req.params 访问数据。

例如,我如何使用这个简单的应用程序从 bodyParser() 迁移到 busboy/connect-busboy:

var express = require('express');
var app = express();
var http = require('http').Server(app);

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

var busboy = require('connect-busboy');
app.use(busboy());

// How to use busboy to prevent multipart files here?
app.post("/form_data_no_fileupload", function(req, res) {
var somedata = req.body.somedata;
});

// Use busboy to handle both regular form data + fileuploads
app.post("/form_data_AND_fileupload", function(req, res) {

});

// What would handle GET without bodyparser?
app.get("/get_something", function(req, res) {
var params = req.params;
});

http.listen(3000, function() {});

最佳答案

[How] I can specify which route should handle multipart data and which should not?

所有 express 'routing methods允许提供特定于路由的中间件。这包括 Router methods .

app.METHOD(path, callback [, callback ...])

根据单个路由所需的正文,您可以使用不同的模块来处理每个模块(而不是使用 app.use() 将它们应用到整个应用程序)。

var express = require('express');
var app = express();
var http = require('http').Server(app);

var bodyParser = require('body-parser');
var busboy = require('connect-busboy');

app.post("/form_data_no_fileupload",
bodyParser.urlencoded(),
function(req, res, next) {
// check that the request's body was as expected
if (!req.body) return next('route'); // or next(new Error('...'));

// ...
});

app.post("/form_data_AND_fileupload",
busboy({
limits: {
fileSize: 10 * 1024 * 1024
}
}),
function(req, res, next) {
// check that the request's body was as expected
if (!req.busboy) return next('route'); // or next(new Error('...'));

// ...
});

// ...

Furthermore, busboy docs says it does not handle GET.

So, I'm even more confused how I would parse params in a GET.

Busboy 和 BodyParser 旨在读入和解析请求的正文,其中 GET and HEAD requests aren't expected to have .

对于此类请求,参数只能在 query-string 范围内传递在 URL 中,Express 会自行解析。您可以通过 req.query 获取它们.

app.get('/get_something', function () {
console.log(req.originalUrl);
// "/get_something?id=1

console.log(req.query);
// { id: "1" }
});

req.params表示路由在路径中匹配的任何占位符。这些可用于任何路线,无论采用何种方法。

app.get('/thing/:id', function (req, res) {
console.log(req.originalUrl);
// "/thing/2"

console.log(req.params);
// { id: "2" }
});

关于javascript - 使用 Busboy 迁移 Express 应用程序中的 bodyParser()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com