【问题标题】:TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array node.jsTypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array node.js 的实例
【发布时间】: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


【解决方案1】:

我遇到了同样的错误。通过从 demofile.html 所在的同一文件夹运行命令来修复它。当我用 node node-learning/test.js 运行它时,我猜它找不到 demofile.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-16
    • 2022-11-10
    • 1970-01-01
    • 2022-01-12
    • 2019-01-26
    • 2020-08-12
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多