我試圖在節點中發送一個http響應以在瀏覽器中打印結果。簡化的源代碼如下。基本上,所有的變量都是在程序的某個地方定義的,所以這不應該成爲問題。當我嘗試運行該腳本,我不斷收到錯誤:在node.js中發送HTTP響應
http.js:783
throw new TypeError('first argument must be a string or Buffer');
TypeError: first argument must be a string or Buffer
所以有人可以熟悉的node.js或JavaScript語法讓我知道是什麼問題?
upload = function(req, res) {
var fileInfos = [obj, obj]; //defined as an array of objects
var counter = 0;
counter -= 1;
if (!counter) {
res.end({files: fileInfos}); //files is defined.
}
};
async.forEach(urls, downloadFile, function (err) { //all params defined.
if(err){
console.error("err");
throw err;
}
else{
http.createServer(function(req, res){
upload(req1, res); //req1 defined as an array of objects.
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
}
});
好點!非常感謝答案! – jensiepoo