0

I continue to have issues with my web app deployment on heroku - the error returned is failed to load resource error 404. It runs perfectly on my local node server but doesn't load css or js files when pushed to heroku. I have seen several answers regarding ruby but a limited amount about node - here is my server code:

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

// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8080;

// set the view engine to ejs
app.set('view engine', 'ejs');

// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));

// set the home page route
app.get('/', function(req, res) {

    // ejs render automatically looks in the views folder
    res.render('index');
});

app.listen(port, function() {
    console.log('Our app is running on http://localhost:' + port);
});

Thanks Immensely!

JamesHandshoe
  • 257
  • 1
  • 3
  • 16
  • So it won't load your index.ejs file? Or it won't load CSS or JS? – Charlie Fish Jul 16 '16 at 02:28
  • prob public directory path is wrong , try debugging that value , you could use var path = require("path"); express.static(path.resolve(__dirname ,"/public")) instead. – cshion Jul 16 '16 at 02:29
  • it won't load the css or js. originally i had index.html but read some where about trying to change it to ejs – JamesHandshoe Jul 16 '16 at 02:30

1 Answers1

1

If your JS and CSS files are in the public directory, you'll need to modify your code to reflect that. The problem is that heroku is looking for those files in the root directory, when they're actually in /public.

To add /public to the request URL, you need to change this line

app.use(express.static(__dirname + '/public'));

to this

app.use("/public", express.static(__dirname + '/public'));

You can read more about this in a related post.

Community
  • 1
  • 1
mdegges
  • 963
  • 3
  • 18
  • 39
  • Also, in the future, doing 'heroku logs --tail' will give you detailed (live) error logs. That should help you debug ^^ – mdegges Jul 16 '16 at 04:26
  • I added that change but still nothing on the heroku server. my file structure is this meditation-app -public -views index.html index.js package.json Procfile https://meditate-app.herokuapp.com/ that is the link if it will help see what is happening. I'm newer to coding with node and this is a first attempt to push to heroku so I'm not 100% what the erros logs mean..thanks fro all the help – JamesHandshoe Jul 16 '16 at 15:30