代码之家  ›  专栏  ›  技术社区  ›  LifeAsPixels

试图使用Node.js读取我的本地html文件时,此错误的来源是什么?

  •  -2
  • LifeAsPixels  · 技术社区  · 4 月前

    这一切都发生在我的一台机器上——服务器和客户端机器都是一样的。我正在w3schools上学习,并已成功完成Node.js示例,直到这次我试图读取一个html文件作为显示内容。如前所述,服务器在控制台中正确启动,光标闪烁,但在尝试通过chrome访问localhost时,我收到了控制台记录的错误以及chrome中的“127.0.0.1拒绝连接”通知

    错误:

    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 undefined
        at write_ (node:_http_outgoing:949:11)
        at ServerResponse.write (node:_http_outgoing:904:15)
        at ReadFileContext.callback (C:\path-to-file-omitted\demo_readfile.js:7:9)
        at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:299:13) {
      code: 'ERR_INVALID_ARG_TYPE'
    }
    
    Node.js v22.13.0
    

    我使用的这两个文件是逐字逐句的 w3schools .

    demofile1.html

    <html>
    <body>
    <h1>My Header</h1>
    <p>My paragraph.</p>
    </body>
    </html>
    

    demo_readfile.js

    var http = require('http');
    var fs = require('fs');
    
    
    
    http.createServer(function (req, res) {
      fs.readFile('demofile1.html', function(err, data) { // read the html file
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data); // write the html content to the view
        return res.end();
      });
    }).listen(8080);
    

    使用Powershell启动node.js实例,并使用“node C:\path to file\filename.js”的文件,我尝试以管理员身份运行Powershell。我试着在谷歌和堆栈溢出上做了一些搜索,以在其他地方发现这个错误,虽然它们看起来很相似,但据我所知,我发现的那些并没有直接适用于我的问题,也没有给我一个解决方案。

    3 回复  |  直到 4 月前
        1
  •  0
  •   Proteus    4 月前

    您提供的代码确实正确。问题是,您使用的是绝对路径,这会让文件系统模块感到困惑。
    为了避免这样的错误,您可以将HTML文件和JS脚本移动到一个目录中,并使用以下命令运行它们 node demo_readfile.js 而不是 node C:\something\demo_readfile.js 。更好的做法是使用文件系统路径实用程序模块,如下所示:

    const http = require("http");
    const path = require("path");
    const fs = require("fs");
    
    http.createServer(function (req, res) {
    
      fs.readFile(path.join(__dirname, "demofile1.html"), function(err, data) {
    
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        return res.end();
    
      });
    
    }).listen(8080);
    

    上述代码利用 path.join() 以确保 fs.readFile() 始终导航到正确的路径,无论从哪里调用脚本(这在处理大型项目时很有用,例如当辅助函数文件位于索引之外的其他位置时)。

        2
  •  0
  •   LifeAsPixels    4 月前

    将powershell工作目录更改为javascript和html文件位置,并使用“node filename.js”启动服务器,而不是使用绝对路径启动服务器,解决了这个问题。在原始帖子的评论中归因于大卫。

        3
  •  -1
  •   Lê Hoàng VÅ©    4 月前

    因为你是新来的,所以让我们来分解一下: 在错误控制台中,注意代码的行点:

    at ReadFileContext.callback (C:\path-to-file-omitted\demo_readfile.js:7:9)
    

    这意味着错误出现在第7行第9个字符中。 接下来,读取错误:

    The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
    

    所以把两者结合起来,我认为你 data 当前未定义,您正在尝试编写 undefined 并返回浏览器。

    那么你应该尝试的解决方案是:

    1. 检查路径。确保你把什么作为第一个论点 fs.readFile 是正确的路径和文件名。您应该将两个文件放在同一个文件夹中。
    2. 在将数据转到浏览器之前,请尝试记录数据:添加 console.log(data) 线之前 res.writeHead... Sample