【问题标题】:Error by using res.sendfile() in nodejs在 nodejs 中使用 res.sendfile() 出错
【发布时间】:2017-06-03 02:03:20
【问题描述】:

我在 nodejs 上有项目,我是 nodejs 的初学者。 当我尝试使用它时

app.get('/', function(req, res) res.sendfile('index.html');});

它告诉我使用 sendFile(calital F)。 当我在 sendFile 中使用大写 F 时,它会显示此错误

TypeError: path must be absolute or specify root to res.sendFile

 在 ServerResponse.sendFile 处

帮帮我怎么办

【问题讨论】:

  • npm 与 node.js 捆绑在一起。在没有安装 node.js 的情况下如何安装 npm
  • 是的,我也安装了 npm 和 nodejs
  • 那你为什么要运行npm install nodejs
  • 您的输出似乎是用于运行npm install nodejs。运行npm install express时的输出是什么?
  • 现在,我没事!但是有这个问题

标签: node.js npm socket.io


【解决方案1】:

您可以尝试以 sudo 身份运行吗?

sudo npm install nodejs

安装节点的更好方法是下载安装程序 https://nodejs.org/en/download/

【讨论】:

    【解决方案2】:

    您应该在系统中输入文件的确切文件路径。

    将此添加到文件顶部:

    var path = require('path');
    

    然后:

    app.get('/', function(req, res){ 
        console.log(path.join(__dirname, 'index.html')); //See the output in console
        res.sendFile(path.join(__dirname, 'index.html')); //this joins your current directory and filename, giving you the full path to the file.
    });
    

    我不知道为什么它不适合你。只需编写“index.html”通常就可以了。但这肯定会奏效。

    【讨论】:

      【解决方案3】:

      试试这个,它对我有用!

              var path = require('path');
              res.sendFile(path.resolve('output.xlsx'));
      

      再见!

      【讨论】:

        猜你喜欢
        • 2018-12-31
        • 2014-11-29
        • 2023-04-03
        • 2014-11-08
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 2021-10-15
        相关资源
        最近更新 更多