heroku部署与节点服务器

问题描述:

我继续有问题与我的网络应用程序部署在heroku上 - 返回的错误是无法加载资源错误404。它完美运行在我的本地节点服务器上,但不会加载css或js文件时推到heroku。我所看到的关于Ruby,但有关节点数量有限的几个答案 - 这里是我的服务器代码:heroku部署与节点服务器

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); 
}); 

非常感谢!

+0

那么它不会加载你的index.ejs文件?或者它不会加载CSS或JS? –

+0

问题公共目录路径错误,请尝试调试该值,您可以使用var path = require(“path”); express.static(path.resolve(__ dirname,“/ public”))。 – cshion

+0

它不会加载CSS或JS。最初我有index.html,但阅读了一些关于尝试将其更改为ejs – JamesHandshoe

如果您的JS和CSS文件位于公共目录中,则需要修改代码以反映该问题。问题在于,当heroku实际处于/ public状态时,它正在根目录中查找这些文件。

要添加/公众对请求的URL,你需要这条线

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

改变这种

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

您可以在related post阅读更多关于这一点。

+0

的地方。而且,在未来,执行'heroku logs -tail'会为您提供详细的(实时)错误日志。这应该有助于你调试^^ – mdegges

+0

我添加了这个改变,但仍然没有在heroku服务器上。 我的文件结构是这样的 沉思应用 -public -views 的index.html index.js 的package.json Procfile https://meditate-app.herokuapp.com/那就是链接,如果它会帮助看看发生了什么。我更喜欢用节点编码,这是第一次尝试推向heroku,所以我不是100%的erros日志的意思..谢谢所有的帮助 – JamesHandshoe