2014-10-06 203 views
0

我收到以下異常,當我使用下面的命令調用/ list方法打雜拋出不確定的異常

'curl -v http://ec2-54-68-202-47.us-west-2.compute.amazonaws.com:8080/list/?name=test' 

錯誤:

110.175.53.235 - - [Sun, 05 Oct 2014 13:08:27 GMT] "GET /list/?name=dafsdf HTTP/1.1" 500 1273 "-" "curl/7.26.0" TypeError: Cannot call method 'on' of undefined

它的罰款,如果我調用「/圖片/上傳'網址。

// Import required modules 
var express = require('express'); 
var morgan = require('morgan'); 

var busboy = require('connect-busboy'); 
var fs = require('fs'); 
var path = require('path'); 



var app = express(); 

app.use(busboy()); 
app.use(morgan('combined')); 

app.get('/list', function(req, res) { 
     console.log("Hit the url /list"); 
     req.busboy.on('field', function(fieldname, val) { 
       //console.log('Field [' +fieldname);    
       res.send("data received"); 
     }); 
     //req.pipe(req.busboy); 
}); 

app.post('/image/upload', function(req, res) { 
     req.busboy.on('file', function(fieldname, file, filename) { 
       ... 
       res.send("file received"); 
     }); 
     req.pipe(req.busboy); 
}); 

// Run the application on port 3000 
app.listen(8080, function() { 
     console.log('Echo Application running at %d', 8080); 
}); 
+0

的話你確定你可以使用打雜用GET請求? – xShirase 2014-10-06 00:10:40

+0

對GET/HEAD /類似請求使用正文解析器是無用的,因爲這些類型的請求不應該具有正文。 – mscdex 2014-10-06 00:58:18

回答

0

Busboy用於解析傳入表單數據,所以它是用於POST請求的。

在得到一個列表,你會更好使用常規語法,即:req.params

mscdex

busboy works for any request, however connect-busboy specifically does not set up req.busboy for GET, HEAD, other requests that don't typically contain a body, or the Content-Type is not application/x-www-form-urlencoded or multipart/form-data

+0

'busboy'適用於任何請求,但'connect-busboy'特別[不設置'req.busboy'](https://github.com/mscdex/connect-busboy/blob/b9175fc77ddcca15e6db30cd309ecabecae76325/index.js#或者'Content-Type'不是'application/x-www-form-urlencoded'或'multipart/form- data'。 – mscdex 2014-10-06 00:43:00

+0

謝謝你比我清楚;) – xShirase 2014-10-06 00:54:10