NodeJS错误与connect-busboy," TypeError:无法调用方法' on'未定义"

时间:2014-11-27 03:22:32

标签: javascript node.js express connect busboy

我有这段代码:

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 个答案:

答案 0 :(得分:4)

问题是您的请求不是multipart/form-data,而是image/gif。 Busboy,multer,强大,多方等都无法解析请求,除非它是multipart/form-data(如果您正在上传文件)。

答案 1 :(得分:1)

确保您拥有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