简体   繁体   English

Heroku Node App资源未加载

[英]Heroku Node App Resources Not Loading

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. 我有一个在本地运行良好的Express Node应用程序,但是当我将其部署到Heroku时, /bower_components /public/bower_components下的文件。

在此处输入图片说明

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.js的相关部分:

app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components',  express.static(__dirname + '/bower_components'));

Here is my package.json file: 这是我的package.json文件:

{
  "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. 这使我相信Heroku正确读取__dirname存在问题。

This issue was solved by this post. 此问题已解决。 Node.js with express app crashes on Heroku, works locally 带有Express应用的Node.js在Heroku上崩溃,在本地工作

I had to move the following lines above the other app configuration lines in my app.js 我必须将以下行移动到我的app.js中其他应用程序配置行的上方

app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components',  express.static(__dirname + '/bower_components'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM