0

我正在尝试使用 busboy 中间件从 POST 请求中获取正文。

我在主 server.js 中有以下内容

//file upload middleware
var busboy = require('connect-busboy');
app.use(busboy());

我有一个这样设置的路线:

app.post('/create', function(req, res){

    req.pipe(req.busboy);
    req.busboy.on('finish', function (fieldname, file, filename) {
        console.log(req.body);
    });

});

但是,如果我向端点发送请求,则会收到以下错误:

_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)

我如何阅读这条路线内的正文?

4

1 回答 1

1

您的finish事件处理程序是错误的。它看起来像一个错字,应该是一个file事件处理程序。

其次,看起来请求Content-Type不正确。它应该是multipart/form-data任何一个application/x-www-form-urlencoded

最后,req.body不会在你的事件处理程序中设置,因为你没有填充它,所以它总是undefined.

于 2015-03-31T06:17:57.750 回答