我正在尝试部署到heroku,但即使它在localhost上运行也得到503。我很好奇,因为我是编程新手,所以服务器设置是否正确。我希望任何人都可以向我指出正确的方向,以便寻找或提出建议,因为我在Google上花费了数小时的时间,长达数周。

我的主要问题是我是否正确设置了服务器?我不确定我的监听器是否可以在heroku上使用,而我的.get在最初设置时是否用于在localhost上进行调试。

我的完整项目也可以在这里找到:

https://github.com/dirkdir/DerekDevSite

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


app.use(express.static(path.join(__dirname, 'public')));


app.get('/json', function(req, res) {
    console.log("GET the json");
    res
        .status(200)
        .json( {"jsonData" : true} );
});

app.get('/file', function(req, res) {
    console.log("GET the file");
    res
        .status(200)
        .sendFile(path.join(__dirname, 'app.js'));
});



    var server = app.listen(process.env.PORT || 5000), function() {
        var port = server.address().port;
        console.log("Express is working on port " + port);
});

日志:

2017-04-24T20:04:43.755866 + 00:00 app [web.1]:在Module._compile(module.js:542:28)
2017-04-24T20:04:43.755867 + 00:00 app [web.1]:位于Object.Module._extensions..js(module.js:579:10)
2017-04-24T20:04:43.755868 + 00:00 app [web.1]:位于Module.load(module.js:487:32)
2017-04-24T20:04:43.755868 + 00:00 app [web.1]:在tryModuleLoad(module.js:446:12)
2017-04-24T20:04:43.755869 + 00:00 app [web.1]:在Function.Module._load(module.js:438:3)
2017-04-24T20:04:43.755869 + 00:00 app [web.1]:在Module.runMain(module.js:604:10)
2017-04-24T20:04:43.755870 + 00:00 app [web.1]:运行时(bootstrap_node.js:393:7)
2017-04-24T20:04:43.755871 + 00:00 app [web.1]:在启动时(bootstrap_node.js:150:9)
2017-04-24T20:04:43.846556 + 00:00 heroku [web.1]:状态从开始更改为崩溃
2017-04-24T20:26:31.826133 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法= GET路径=“/” host = derekdyerdev.herokuapp.com request_id = 609ef253-0a56 -41ac-b877-1fb242f6f4e1 fwd =“69.36.89.218” dyno = connect =服务=状态= 503字节=协议(protocol)= https
2017-04-24T20:26:32.319732 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法= GET路径=“/favicon.ico”主机= derekdyerdev.herokuapp.com request_id = f2a34e62-9765-

最佳答案

您的34文件的app.js行上的括号放在错误的位置。
由于该函数是一个回调,因此它必须在params之内。

将最后一个块更改为此:

var server = app.listen(process.env.PORT || 5000, function () {
  var port = server.address().port;
  console.log("Express is working on port " + port);
});

我也将Procfile修改为:
web: node app.js

修改后,我可以在本地运行,然后将其部署到Heroku中只是为了进行测试,并且效果很好:)

关于node.js - 尝试打开W​​eb应用程序时,Heroku给我503,可在本地主机上使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43596835/

10-16 21:52