【问题标题】:ENOENT: no such file or directory HerokuENOENT:没有这样的文件或目录 Heroku
【发布时间】:2013-07-28 09:16:41
【问题描述】:

我正在尝试使用 readFileSync() 将 html 文件的内容显示到节点 js 文件中。但我得到这个错误:

Error: ENOENT, no such file or directory 'index.html'
    at Object.fs.openSync (fs.js:427:18)
    at Object.fs.readFileSync (fs.js:284:15)
    at port (/app/web.js:6:22)
    at callbacks (/app/node_modules/express/lib/router/index.js:161:37)
    at param (/app/node_modules/express/lib/router/index.js:135:11)
    at pass (/app/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/app/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/app/node_modules/express/lib/router/index.js:33:10)
    at next (/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.expressInit [as handle] (/app/node_modules/express/lib/middleware.js:30:5)

这是我的 web.js 文件:

var express = require('express');
var app = express.createServer(express.logger());
var fs=require('fs');
app.get('/', function(request, response) {
//  response.send('Hello World2 !');
response.send(fs.readFileSync('index.html').toString());
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
 console.log("Listening on " + port);
});

“web.js”文件和“index.html”在我的github仓库中,地址如下:

https://github.com/myUsername/bitstarter/blob/master/node-js-sample/web.js 
https://github.com/myUsername/bitstarter/blob/master/node-js-sample/index.html

我尝试了不同的路径来解决“index.html”,但都不起作用,例如

 /bitstarter/blob/master/node-js-sample/index.html
/node-js-sample/index.html
/bitstarter/node-js-sample/index.html
./index.html

! 我正在使用 AWS EC2 ubuntun 12-4。 我已经为这个错误苦苦挣扎了好几个小时!非常感谢任何帮助。

【问题讨论】:

    标签: node.js heroku github


    【解决方案1】:

    app.get('/', ... 的意思是“当客户端请求 '/'...”即:它将“网络空间”(URL 路径)映射到操作。在您的情况下 - 读取并返回 index.html 的内容。

    因此,在您的情况下,正确的请求就是 www.mydomain.com/

    如果您希望能够提供静态文件,请阅读express.static()

    【讨论】:

      【解决方案2】:

      正如 Nitzan 所提到的,从长远来看,您将需要学习如何在构建节点应用程序时使用 express.static() 来提供 html 文件和模板。但是,针对您当前的问题,我建议如下:

      1. index.html 文件保存到与 EC2 实例中的 web.js 文件相同的目录中(即不要尝试在您的 github 页面上远程访问它)。

      2. 使用以下代码:

      var content = fs.readFileSync('index.html', 'utf-8');
      response.send(内容);

      这会将index.html 文件的内容读入一个名为content 的变量中,同时指定正确的编码。然后它会指示节点将内容发送到浏览器。

      【讨论】:

        【解决方案3】:

        问题似乎是您没有在 git 中添加“index.html”。拳头你需要将它添加到 git 然后推送到 heroku。这将解决您的问题。希望对你有帮助:)

        【讨论】:

          猜你喜欢
          • 2021-03-30
          • 1970-01-01
          • 2018-07-03
          • 2014-01-12
          • 1970-01-01
          • 2023-03-31
          • 2017-01-24
          • 2023-03-29
          • 2019-02-03
          相关资源
          最近更新 更多