I have an Express Node app that runs fine locally, but when I deploy to Heroku files under /public
and /bower_components
are not found.
This post seemed to have a similar problem, but the solution did not work for me.
Here is the relevant part of my app.js:
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
Here is my package.json file:
{
"name": "MyApp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"postinstall": "bower install"
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"consolidate": "~0.10.0",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"bower": "^1.3.12",
"dustjs-helpers": "~1.4.0",
"dustjs-linkedin": "~2.5.0",
"node-compass": "0.2.3"
},
"engines": {
"npm": "2.7.1"
}
}
In the browser console I can verify that all the files are present
This leads me to believe there is an issue with Heroku reading __dirname correctly.
This issue was solved by this post. Node.js with express app crashes on Heroku, works locally
I had to move the following lines above the other app configuration lines in my app.js
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.