3

New to Node.js and Heroku, recently got it set up and working with a basic hello world, but now I'm trying to get Node.js to display an HTML file, and am having issues. Here's my single JS file:

web.js

var express = require('express');
var app = express();

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

app.get('/', function(req, res) {
    res.render('about.html');
});

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

My HTML file is in '/views', and just displays basic text/title, and works FINE when running 'node web.js'. However, when uploading to Heroku and visiting my site, I get the following error:

Error: Failed to lookup view "about.html" in views directory "/app/views" at Function.app.render (/app/node_modules/express/lib/application.js:508:17) at ServerResponse.res.render (/app/node_modules/express/lib/response.js:782:7) at Object.handle (/app/web.js:9:6) at next_layer (/app/node_modules/express/lib/router/route.js:103:13) at Route.dispatch (/app/node_modules/express/lib/router/route.js:107:5) at /app/node_modules/express/lib/router/index.js:213:24 at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:284:12) at next (/app/node_modules/express/lib/router/index.js:207:19) at Layer.expressInit [as handle] (/app/node_modules/express/lib/middleware/init.js:23:5) at trim_prefix (/app/node_modules/express/lib/router/index.js:252:17)

I'm confused by it saying '/apps/views', as with the original error, the directory was named '/Views', but I changed it to see if it'd help, to no avail. I don't have an /apps directory, is that something they make?

Anyway, just want to display some simple HTML from a subfolder using Node.js on Heroku. Any help is appreciated.

4
  • Is your package.json correct? To test, try pulling the git repo (or moving all files except for the node_modules folder) on a different machine/environment and running npm install followed by node web.js Commented Jun 15, 2014 at 4:14
  • Tested it twice, works on two different environments. The problem seems to lie on the side of Heroku's server, any further ideas? Commented Jun 15, 2014 at 16:35
  • Did you follow their guide in the devcenter for the procfile settings? They have fairly specific requirements and any variation could cause this behavior. Commented Jun 15, 2014 at 16:53
  • My procfile has one line, "web: npm install & node web.js", not sure if more is needed but everything is working locally. It IS seeing the content in the static directory, but not finding the /views directory at all. Any ideas? Commented Jun 15, 2014 at 21:48

2 Answers 2

1

This is probably a very very late answer, but you were on the right path with the changing of the /Views to /views, as file paths in Heroku are indeed case sensitive, unlike in your local environment (hence why it works when you do a local run). The only thing is that was not properly pushed into Heroku because of cache. So what you should have done/do is had to/to delete the file, push once without it inside Heroku, so it would make note of the file change, and then redo another push with the correct views /views included.

Similarly your __dirname is defined in Heroku with an /app. If you run heroku run bash and then run pwd you will see that your current working directory is /app, hence why /app is added in front of all your paths.

So when doing a ls in your bash you should do a cd to your folder /app and inside it see /views and then with another cd to /views check to see if the about.html is inside that folder. If it is not, then you did not do the push correctly and that is why you have the error. But if you push correctly (like for example by pushing all files without the /views folder or about.html file, with the right case sensitive names, your problem will be fixed.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks !!!, removing the views folder and then adding again worked !!!. This answer saved my life.
My pleasure. Glad to hear that @SudheerTripathi :)
0

in the exception stack it says the error is in /app/web.js, so I would say the app dir is the root dir on heroku where all your files are stored, and not a dir where your views are stored which would make sense. Can you confirm the html file is indeed uploaded to heroku?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.