简体   繁体   English

用节点服务器部署heroku

[英]heroku deployment with node server

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. 我在heroku上的Web应用程序部署仍然存在问题-返回的错误未能加载资源错误404。该错误在我的本地节点服务器上完美运行,但是在推送到heroku时不加载css或js文件。 I have seen several answers regarding ruby but a limited amount about node - here is my server code: 我已经看到了一些关于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);
});

Thanks Immensely! 非常感谢!

If your JS and CSS files are in the public directory, you'll need to modify your code to reflect that. 如果您的JS和CSS文件位于公共目录中,则需要修改代码以反映出来。 The problem is that heroku is looking for those files in the root directory, when they're actually in /public. 问题在于,heroku实际上在/ public中时,正在根目录中查找这些文件。

To add /public to the request URL, you need to change this line 要将/ public添加到请求URL,您需要更改此行

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 . 您可以在相关文章中阅读有关此内容的更多信息

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

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