NodeJS与连接busboy错误,“TypeError:不能调用方法”的未定义“

我有这段代码:

router.route("/post") .get(function(req, res) { ... }) .post(authReq, function(req, res) { ... // Get uploaded file var fstream req.pipe(req.busboy) req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { ... var fileSuffix = mimetype.split("/")[1] // Get suffix, may not always be correct var tmpFileName = postCount + "." + fileSuffix var tmpFileURL = __dirname + "/tmp/" + tmpFileName // Start writing fstream = fs.createWriteStream(tmpFileURL) file.pipe(fstream) fstream.on('close', function() { ... }) }) }) }) 

这个使用完美的工作。 但是,我不知道发生了什么事,但是今天我拿起电脑,发现每次尝试发布时都会发生这样的错误:

 TypeError: Cannot call method 'on' of undefined at IncomingMessage.Readable.pipe (_stream_readable.js:494:8) at /Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:139:8 at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5) at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13) at authReq (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:17:3) at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5) at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13) at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:94:14) at Route.dispatch (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:81:3) at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5) _stream_readable.js:501 dest.end(); ^ TypeError: Cannot call method 'end' of undefined at IncomingMessage.onend (_stream_readable.js:501:10) at IncomingMessage.g (events.js:180:16) at IncomingMessage.emit (events.js:92:17) at _stream_readable.js:943:16 at process._tickCallback (node.js:419:13) 

在我的index.js文件中,我有这个代码来使用Busboy:

 app.use(busboy()) 

然后这个代码包含我的路线:

 app.use("/api", require("./apiRoutes.js")()) 

提前致谢!

编辑:我正在做更多的debugging,我想我指出了这个问题。 req.busboy没有定义。 所以我去挖掘源代码。 以下是connect-busboy模块中的一些代码:

 if (req.busboy || req.method === 'GET' || req.method === 'HEAD' || !hasBody(req) || !RE_MIME.test(mime(req))) return next() 

当我打印出实际值时,busboy不存在,请求方法是'POST', hasBody(req)结果为真,但是RE_MIME.test(mime(req)))结果为false,这就是为什么busboy没有被添加到请求。

我的示例文件的MIMEtypes是image/gif 。 这里是正则expression式connect-busboy正在testingimage/gif off(又名RE_MIMEvariables):

 /^(?:multipart\/.+)|(?:application\/x-www-form-urlencoded)$/i; 

因此,我误解了我的判断。 Busboy仅适用于HTML表单吗?

编辑#2:我只是切换到Multer ,它工作正常。 不过,我相信我的问题是我需要将文件放在请求的多部分中, 我不知道为什么它以前工作。 谢谢!

问题是你的请求不是multipart/form-data ,而是image/gif 。 除非是multipart/form-data (如果你正在上传文件),Busboy,Muller,强大的,多方的等等不能parsing请求。

确保你有你的index.js文件:

 var busboy = require('connect-busboy'); // ... var app = express(); // ... // and app.use(busboy()); // I had exact the same error when this line was missing in index.js