Node.js v21.7.1 返回静态文件时出现ERR_INVALID_ARG_TYPE错误求助
Node.js静态文件返回时出现ERR_INVALID_ARG_TYPE错误排查
使用Node.js v21.7.1开发Web服务器,返回如jquery/jquery-2.2.1.min.js这类静态文件时,控制台抛出如下错误:
L392 [strFilename] typeof:string value:[/scripts/jquery/jquery-2.2.1.min.js] 2024-03-22 10:21:42.566 HTTP request for: /scripts/jquery/jquery-2.2.1.min.js from: localhost:8000 2024-03-22 10:21:42.566 Full path: C:\www\scripts\jquery\jquery-2.2.1.min.js L438 C:/www/scripts/jquery/jqwidgets/styles/jqx.base.css L441 File: C:/www/scripts/jquery/jqwidgets/styles/jqx.base.css text/css node:_http_outgoing:949 throw new ERR_INVALID_ARG_TYPE( ^ TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type number (200) at write_ (node:_http_outgoing:949:11) at ServerResponse.write (node:_http_outgoing:908:15) at C:\www\server.js:442:38 at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) { code: 'ERR_INVALID_ARG_TYPE' } Node.js v21.7.1
对应的处理代码:
fs.exists(strFullPath, function(exists) { if ( !exists ) { response.writeHead(404, {"Content-Type":"text/plain"}); response.write("404 Not Found\n"); response.end(); return; } fs.readFile(strFullPath, "binary", function(err, file) { if ( err ) { console.log(consts.RED + "L434 " + consts.RESET + strFullPath);//HACK response.writeHead(500, {"Content-Type":"text/plain"}); response.write(err + "\n"); response.end(); return; } console.log(consts.RED + "L438 " + consts.RESET + strFullPath);//HACK const cstrExt = path.extname(strFullPath) ,cstrType = cstrContentTypes[cstrExt]; if ( typeof cstrType == "string" ) { console.log(consts.RED + "L441 File: " + consts.RESET + strFullPath + " " + cstrType); response.write(200, {"Content-Type":cstrType}); response.write(file, "binary"); response.end(); } }); });
错误原因
代码第442行误用了response.write方法:response.write(200, {"Content-Type":cstrType})。response.write的第一个参数是要发送给客户端的内容(chunk),要求是字符串、Buffer或Uint8Array类型,传入数字200直接触发了类型错误。设置HTTP状态码和响应头应该用response.writeHead方法。
修正后的代码
把错误的response.write替换为response.writeHead即可:
fs.exists(strFullPath, function(exists) { if ( !exists ) { response.writeHead(404, {"Content-Type":"text/plain"}); response.write("404 Not Found\n"); response.end(); return; } fs.readFile(strFullPath, "binary", function(err, file) { if ( err ) { console.log(consts.RED + "L434 " + consts.RESET + strFullPath);//HACK response.writeHead(500, {"Content-Type":"text/plain"}); response.write(err + "\n"); response.end(); return; } console.log(consts.RED + "L438 " + consts.RESET + strFullPath);//HACK const cstrExt = path.extname(strFullPath) ,cstrType = cstrContentTypes[cstrExt]; if ( typeof cstrType == "string" ) { console.log(consts.RED + "L441 File: " + consts.RESET + strFullPath + " " + cstrType); // 修正:使用writeHead设置状态码和响应头 response.writeHead(200, {"Content-Type":cstrType}); response.write(file, "binary"); response.end(); } }); });
额外优化建议:fs.exists已被废弃,推荐使用fs.access或直接在fs.readFile中捕获文件不存在的错误,避免额外的文件系统调用。
内容的提问来源于stack exchange,提问作者SPlatten
相关产品推荐
相关产品推荐

