簡體   English   中英

Heroku不會加載index.html

[英]Heroku won't load index.html

我正在嘗試用Heroku創建一個node.js應用程序。 在我的server.js文件中,我使用express加載靜態html頁面。 這是我的server.js文件中的快速代碼。 在Heroku的procfile中,將其設置為我的起點。

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

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

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

但是當我將其推送到Heroku時,Heroku不會顯示任何內容,因此也不會顯示任何錯誤。

server.js,index.html位於同一文件夾中

這就是您的整個server.js嗎? 如果是這樣,則需要指定要偵聽的端口:

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

編輯:正如@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

Heroku雪松堆棧上的Node.js端口問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM