【发布时间】:2023-03-10 06:55:01
【问题描述】:
您好,下面的 javascript 代码允许我从文件系统中恢复文件并将它们发送到前端,但是,当我运行代码时出现以下错误,这是什么原因造成的?
错误: TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或 Array-like Object 类型之一。收到的类型对象,在此代码上
JavaScript 代码:
http.createServer(function(req, res) {
console.log("Recupero immagini");
var request = url.parse(req.url, true);
var action = request.pathname;
//Recupero il logo della società
if (action == '/logo.jpg') {
console.log("Recupero logo");
var img = fs.readFileSync('./Controller/logo.jpg');
res.writeHead(200, {
'Content-Type': 'image/jpeg'
});
res.end(img, 'binary');
}
//Recupero la firma del tecnico
else if (action == '/firmatecnico.png') {
console.log("Recupero logo tecnico");
var img2 = fs.readFileSync('./firmatecnico.png');
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(img2, 'binary');
}
}).listen(8570);
【问题讨论】:
-
在哪一行抛出错误?
-
@eol on res.end 行和 fs.readFileSync
-
虽然这并不理想(您正在将整个文件读入内存并且应该使用流/管道),但您的代码在我的机器上运行良好。你可以发布堆栈跟踪吗?