W3 School Node.js文件系统模块中的示例在我的机器上给出这个错误:
node:_http_outgoing:791
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 undefined
←[90m at new NodeError (node:internal/errors:377:5)←[39m
←[90m at write_ (node:_http_outgoing:791:11)←[39m
←[90m at ServerResponse.write (node:_http_outgoing:756:15)←[39m
at ReadFileContext.callback (C:Proyectosmyfirst.js:6:9)
←[90m at FSReqCallback.readFileAfterOp
我已经安装了Node.js v18.3.0并完全按照页面复制粘贴的方式写下来。我已经检查了相关(类似)的答案,并没有提供解决方案,在我的情况下。
代码:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
html:
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
我得到了同样的错误。通过在demofile.html所在的文件夹中运行命令来修复它。当我用node-learning/test.js运行它时,我猜它找不到demofile.html