我有一段代码:
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")())
先谢谢你了!
**编辑:**我做了更多的调试,我想我找到了问题。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)
结果为true,而RE_MIME.test(mime(req)))
结果为false,这就是为什么busboy没有添加到请求中。
我的示例文件的MIME类型是image/gif
。下面是正则表达式connect-busboy
测试image/gif
关闭(又名RE_MIME
变量):
/^(?:multipart\/.+)|(?:application\/x-www-form-urlencoded)$/i;
因此,我得出结论,我误解了API。Busboy是否只支持HTML表单?
**编辑#2:**我只是切换到Multer,它工作正常。然而,我相信我的问题是,我需要把文件放在一个多部分的请求;我不知道为什么它以前是工作的。谢谢!
2条答案
按热度按时间5lhxktic1#
问题是您的请求不是
multipart/form-data
,而是image/gif
。杂工,杂工,令人生畏的,多方的,等等无法解析请求,除非它是multipart/form-data
(如果您正在上传文件)。63lcw9qa2#
确保你的index.js文件中有: