Reputation: 257
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!
Upvotes: 0
Views: 590
Reputation: 963
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.
Upvotes: 1