简体   繁体   English

Heroku不会加载index.html

[英]Heroku won't load index.html

I'm trying to create a node.js application with Heroku. 我正在尝试用Heroku创建一个node.js应用程序。 In my server.js file I use express to load a static html page. 在我的server.js文件中,我使用express加载静态html页面。 This is the express code in my server.js file. 这是我的server.js文件中的快速代码。 In the procfile from Heroku I set this as my starting point. 在Heroku的procfile中,将其设置为我的起点。

var app = require('express')();

var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendFile(__dirname + 'index.html');
});

But when I push it to Heroku, Heroku doesn't show anything, so also no errors. 但是当我将其推送到Heroku时,Heroku不会显示任何内容,因此也不会显示任何错误。

server.js, index.html are in the same folder server.js,index.html位于同一文件夹中

Is that your entire server.js ? 这就是您的整个server.js吗? If so, you need to specify a port to be listened to: 如果是这样,则需要指定要侦听的端口:

var app = require('express')();
var server = require('http').Server(app);
server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});
console.log('Express server started on port %s', server.address().port);

EDIT: As @Abdelouahab suggested, you shouldn't hardcode the port, rather grab it from process.env.PORT 编辑:正如@Abdelouahab建议的那样,您不应该对端口进行硬编码,而应从process.env.PORT中获取它。

var port = process.env.PORT || 3000;
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});
app.listen(port, function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

https://devcenter.heroku.com/articles/getting-started-with-nodejs#push-local-changes https://devcenter.heroku.com/articles/getting-started-with-nodejs#push-local-changes

Node.js port issue on Heroku cedar stack Heroku雪松堆栈上的Node.js端口问题

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

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