2017-04-14 97 views
0

要限制请求大小为123个字节,我用它工作正常的情况如下:快递:限制请求大小

app.use(bodyParser.urlencoded({ 
    extended: true, 
    limit: 123 
})); 

解析所有请求除了“/ specialRequest”,我用下面这正常工作,以及:

app.use(/^(?!\/specialRequest)/,bodyParser.urlencoded({ 
    extended: true 
})); 

但我不能限制所有请求123的请求大小和解析所有的请求除了“/ specialReq uest”。这里是我当前的代码:

app.use(/^(?!\/specialRequest)/,bodyParser.urlencoded({ 
    extended: true, 
    limit: 123 
})); 

然而,这只是限制了除“/ specialRequest”不同的请求,则请求的大小。我怎样才能将所有请求的请求大小限制为123,并解析除“/ specialRequest”之外的所有请求

回答

1

如果您希望/specialRequest的请求正文大小受限但未解析,则可以使用bodyParser.raw()。在这种情况下,req.body将是一个Buffer实例,其中包含请求正文(未解析,尽管如果呈现为压缩或缩小的数据,则会被夸大;此行为可通过其选项禁用)。

您需要在插入bodyParser.urlencoded()中间件之前声明它:

app.post('/specialRequest', bodyParser.raw({ limit : 123, type : '*/*' }), function(req, res) { 
    ... 
}); 

app.use(bodyParser.urlencoded({ 
    extended: true, 
    limit: 123 
})); 
+0

但“/ specialRequest”的大小这种做法不会限制到123? – BJPrim

+0

@BJPrim我想我误解了,看我的编辑。 – robertklep

+0

是的,这是我的意图,但是添加bodyParser.raw({limit:123})没有任何效果。我把它放在bodyParser.urlencoded()之前,但没有显示错误和警告。 – BJPrim