2013-07-28 71 views
0

我想使用readFileSync()将html文件的内容显示到节点js文件中。但是,我得到这个错误:ENOENT:没有这样的文件或目录Heroku

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。 我一直在这个错误几个小时!任何帮助,高度赞赏。

回答

2

app.get('/', ...这句话的意思是“当客户端要求'/'...”时:即:它将“网络空间”(URL路径)映射为动作。在你的情况 - 阅读并返回index.html的内容。

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

如果你希望能够为静态文件,阅读了关于express.static()

1

正如Nitzan提到的,在你将要学习如何使用express.static()来提供HTML文件和模板从长远来看,当你建立你的节点应用程序。在回答你目前的问题,不过,我提出以下建议:

  1. 保存index.html文件在同一目录,如您在web.js文件EC2实例(也就是不要试图在远程访问你的github页面)。

  2. 使用以下代码:

变种含量= fs.readFileSync( '的index.html', 'UTF-8');
response.send(content);

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

0

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

相关问题