简体   繁体   English

未定义busboy与expres的connect-busboy

[英]busboy is not defined connect-busboy with expres

I am working with expressjs 4.12.3, and trying to connect to connect-busboy, but on request I am not able to get req.busboy object, it says "undefined" my simple code is as follows : 我正在使用expressjs 4.12.3,并尝试连接到connect-busboy,但是根据请求,我无法获取req.busboy对象,它说“未定义”,我的简单代码如下:

 var express=require('express'); var busboy = require('connect-busboy'); var app=express(); app.use(busboy()); app.use(function(req, res, next) { req.busboy.on('field', function(fieldname, val) { // console.log(fieldname, val); req.body[fieldname] = val; }); req.busboy.on('finish', function(){ next(); }); }); app.listen(5555); 

I have initialize busboy module, assigned it to the app, also sending content-length: "5276" content-type:'application/x-www-formurlencoded' as headers. 我已经初始化了busboy模块,将其分配给了应用程序,还发送了内容长度:“ 5276”内容类型:“ application / x-www-formurlencoded”作为标题。

what am I doing wrong?? 我究竟做错了什么??

The problem is that you're setting up event handlers, but you're not actually piping the request to busboy so it can parse the request. 问题在于您正在设置事件处理程序,但实际上并没有将请求传递给 busboy,因此它可以解析请求。 Add req.pipe(req.busboy); 添加req.pipe(req.busboy); after your busboy event handlers and it should work fine. 在您的busboy事件处理程序之后,它应该可以正常工作。

EDIT: I slightly misread your question. 编辑:我稍微读错了你的问题。 If req.busboy is undefined that means the Content-Type is wrong. 如果undefined req.busboy则表示Content-Type错误。 If your Content-Type really is application/x-www-formurlencoded , then that is wrong. 如果您的Content-Type 确实是 application/x-www-formurlencoded ,那是错误的。 It should be: application/x-www-form-urlencoded . 应该是: application/x-www-form-urlencoded

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

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