繁体   English   中英

用节点服务器部署heroku

[英]heroku deployment with node server

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

非常感谢!

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

要将/ public添加到请求URL,您需要更改此行

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

对此

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

您可以在相关文章中阅读有关此内容的更多信息

暂无
暂无

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

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