【发布时间】:2016-11-19 07:32:46
【问题描述】:
我在 heroku 上的 web 应用程序部署仍然存在问题 - 返回的错误是无法加载资源错误 404。它在我的本地节点服务器上完美运行,但在推送到 heroku 时不加载 css 或 js 文件。我已经看到了几个关于 ruby 的答案,但关于节点的数量有限 - 这是我的服务器代码:
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);
});
非常感谢!
【问题讨论】:
-
所以它不会加载你的 index.ejs 文件?或者它不会加载 CSS 或 JS?
-
prob 公共目录路径错误,尝试调试该值,可以使用 var path = require("path"); express.static(path.resolve(__dirname ,"/public")) 代替。
-
它不会加载css或js。最初我有 index.html 但阅读了一些关于尝试将其更改为 ejs 的地方