简体   繁体   English

Node.js Connect中间件限制不起作用

[英]Node.js Connect middleware Limit not working

I have a problem server: 我的服务器有问题:

var connect = require('connect');
var http = require('http');

var app = connect()
.use(connect.limit('32kb'))
.use(connect.urlencoded())
.use(connect.json())
.use(function(req, res){
    console.log('yo');
    res.end('hello\n');
});

http.createServer(app).listen(3000);

client: 客户:

var http = require('http');

var req = http.request({
method: 'POST',
port: 3000,
headers: {
    'Content-Type': 'application/json'
}
});

req.write('[');
var n = 30000000;
while (n--) {
req.write('"foo",');
}
req.write('"bar"]');
req.end();

Connect's middleware limit not "limiting" size of json. Connect的中间件限制不“限制” json的大小。 I know that it will deprecated, but instead Express framework what can i use to limit a size of requests? 我知道它会被弃用,但是Express框架可以用来限制请求的大小吗?

do this instead: 改为这样做:

.use(connect.urlencoded({
  limit: '32kb'
}))
.use(connect.json({
  limit: '32kb'
}))

or just: 要不就:

.use(connect.bodyParser({
  limit: '32kb'
})

you can still write to req , but that doesn't necessarily mean the server will receive those bytes. 您仍然可以写入req ,但这并不一定意味着服务器将接收这些字节。 if you check the response and it isn't a 4xx error, then it's a bug. 如果您检查响应,但不是4xx错误,则说明是错误。

EDIT: 编辑:

req.once('response', function (res) {
  assert.equal(res.statusCode, 413)
})
.write(new Buffer(123123123213223122))
.end()

the limit middleware checks the header Content-Length of request message only while there's no such header out of your HTTP POST request. 限制中间件在HTTP POST请求中没有此类标头时才检查请求消息的标头Content-Length

You can verify this by using: 您可以使用以下方法进行验证:

var app = connect()
.use(connect.limit('32kb'))
.use(connect.urlencoded())
.use(connect.json())
.use(function(req, res){
  console.log(req.headers);
  console.log('yo');
  res.end('hello\n');
});

You can test the limit middle via this code: 您可以通过以下代码测试limit中间值:

var http = require('http');

var req = http.request({
  method: 'POST',
  port: 3000,
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': 40 * 1024
  }
});

BTW: you can check how the limit middleware is tested: https://github.com/senchalabs/connect/blob/master/test/limit.js 顺便说一句:您可以检查limit中间件的测试方式: https : //github.com/senchalabs/connect/blob/master/test/limit.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM