【发布时间】:2022-07-13 00:21:26
【问题描述】:
W3学校的例子Node.js File System Module 在我的机器上出现此错误:
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:\Proyectos\myfirst.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>
【问题讨论】:
-
请将您的代码添加到您的问题中。
-
@AbdurrahimAhmadov 我放置了一个代码链接,但无论如何我都会在这里添加。 w3school链接是一样的
-
我认为here 回答了这种情况下的类似问题。你看了吗?
标签: node.js filesystems