eli
eli

Reputation: 21

ENOENT: no such file or directory Heroku

I'm trying to display the content of a html file into node js file using readFileSync(). But I get this error :

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)

Here is my web.js file:

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);
});

The "web.js" file and the "index.html" are in my github repository in the following address:

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

I tried different paths to address "index.html" none worked, such as

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

! I'm using AWS EC2 ubuntun 12-4. I've been straggling with this error for hours! Any help is highly appreciated.

Upvotes: 1

Views: 3781

Answers (3)

surfearth
surfearth

Reputation: 3147

As Nitzan mentioned, in the long run you will want to learn how to use express.static() to serve html files and templates as you build out your node app. In response to your present question, however, I recommend the following:

  1. Save the index.html file into the same directory as your web.js file in you EC2 instance (i.e. don't try to remotely access it on your github page).

  2. Use the following code:

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

This will read the contents of your index.html file into a variable called content while specifying the proper encoding. Then it will instruct node to send the content to the browser.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 435

The problem appears to be that you are not adding "index.html" in git. Fist you need to add this to git and then push to heroku. This will solve your problem. Hope it helps :)

Upvotes: 0

Nitzan Shaked
Nitzan Shaked

Reputation: 13598

The line that says app.get('/', ... means "when the client asks for '/'..." That is: it maps "web space" (URL paths) into actions. In your case -- read and return the contents of index.html.

So the proper request in your case is just www.mydomain.com/.

If you want to be able to serve static files, read up on express.static()

Upvotes: 2

Related Questions