I am developing a web-server application using Node.js v21.7.1, one of the files that is served up is "jquery/jquery-2.2.1.mn.js", my code reads this file then sends it out as a response to the client, however at the start of this file, I see in the console:
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:wwwscriptsjqueryjquery-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:wwwserver.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
The lines before:
node:_http_outgoing:949
Are my debug lines where I’m trying to establish what the problem is and what its caused by. Can anyone help? This is the code that is used with the "fs" node module:
fs.exists(strFullPath, function(exists) {
if ( !exists ) {
response.writeHead(404, {"Content-Type":"text/plain"});
response.write("404 Not Foundn");
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();
}
});
});
2
Answers
I found the issue and it was down to a new version of node being used, the correct syntax for the response write now:
This line contains the arguments for
writeHead
, not forwrite
.The error thrown is correct; an integer (
200
) is not a string or buffer.