0

我有这段代码:

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)结果为真,但RE_MIME.test(mime(req)))结果为假,这就是为什么没有将 busboy 添加到请求中的原因。

我的示例文件的 MIME 类型是image/gif. 这是正则表达式connect-busboy正在测试image/gif(又名RE_MIME变量):

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

因此,我得出的结论是我误解了 API。Busboy 是否仅适用于 HTML 表单?

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

4

2 回答 2

4

问题是您的请求不是multipart/form-dataimage/gif而是。Busboy、multer、formidable、multiparty 等无法解析请求,除非它是multipart/form-data(如果您正在上传文件)。

于 2014-11-27T11:23:53.803 回答
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
于 2014-11-27T10:52:06.703 回答