node.js Heroku 在尝试打开我的网络应用程序时给了我一个 503,在本地主机上工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43596835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:11:08  来源:igfitidea点击:

Heroku is giving me a 503 when trying to open my web app, works on local host

node.jsexpressheroku

提问by Derek Dyer

I am trying to deploy to heroku but get a 503 even though it runs on localhost. I am curious as to if my server is set up correctly as I am newer to programming. I hope anyone can point me in the correct direction as to where to look or provide a suggestion as I have spent countless hours on google to this point spanning a couple of weeks.

我正在尝试部署到 heroku,但即使它在 localhost 上运行,也会得到 503。我很好奇我的服务器是否设置正确,因为我是编程新手。我希望任何人都可以指出我在哪里查看或提供建议的正确方向,因为我已经在谷歌上花费了无数个小时,跨越了几个星期。

My main question is if I have set up my server correctly? I am not sure my listener will work for heroku and my .get are used for debugging on localhost when it was initially set up.

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

Also my full project is available here:

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

https://github.com/dirkdir/DerekDevSite

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

Logs:

日志:

2017-04-24T20:04:43.755866+00:00 app[web.1]: at Module._compile (module.js:542:28) 2017-04-24T20:04:43.755867+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10) 2017-04-24T20:04:43.755868+00:00 app[web.1]: at Module.load (module.js:487:32) 2017-04-24T20:04:43.755868+00:00 app[web.1]: at tryModuleLoad (module.js:446:12) 2017-04-24T20:04:43.755869+00:00 app[web.1]: at Function.Module._load (module.js:438:3) 2017-04-24T20:04:43.755869+00:00 app[web.1]: at Module.runMain (module.js:604:10) 2017-04-24T20:04:43.755870+00:00 app[web.1]: at run (bootstrap_node.js:393:7) 2017-04-24T20:04:43.755871+00:00 app[web.1]: at startup (bootstrap_node.js:150:9) 2017-04-24T20:04:43.846556+00:00 heroku[web.1]: State changed from starting to crashed 2017-04-24T20:26:31.826133+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=derekdyerdev.herokuapp.com request_id=609ef253-0a56-41ac-b877-1fb242f6f4e1 fwd="69.36.89.218" dyno= connect= service= status=503 bytes= protocol=https 2017-04-24T20:26:32.319732+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=derekdyerdev.herokuapp.com request_id=f2a34e62-9765-

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: 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[ .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[路由器]:at=error code=H10 desc="App crashed" method=GET path="/" host=derekdyerdev.herokuapp.com request_id=609ef253-0a56-41ac-b877-1fb242f6f4e1 fwd="69.36.89.218" dyno= connect= service = status=503 bytes= protocol=https 2017-04-24T20:26:32.319732+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico"主机=derekdyerdev.herokuapp.com request_id=f2a34e62-9765-

采纳答案by Alicia

You have the bracket on line 34of your app.jsfile in the wrong place. Since the function is a callback, it needs to be within the params.

34app.js文件行上的括号放在错误的位置。由于该函数是一个回调,所以它需要在 params 内。

Change that last block to this:

将最后一个块更改为:

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

I also modified the Procfileto just this:

我也修改了Procfile这个:

web: node app.js

After I modified that I was able to run locally, and I deployed it to my Heroku just to test, and it works fine :)

在我修改后我可以在本地运行,我将它部署到我的 Heroku 只是为了测试,它工作正常:)

回答by James

I had a similar issue. Worked on localhost.. not when deployed to Heroku. My problem? I had cloned a project (node_modules too) and my package.json did not have express, minimist etc.. so it obviously failed.

我有一个类似的问题。在本地主机上工作......而不是在部署到 Heroku 时。我的问题?我克隆了一个项目(也有 node_modules),而我的 package.json 没有 express、minimist 等,所以它显然失败了。

Tip:When deploying, view the logs right away so you can see what's happening throughout the deployment process. Viewing the logs after it has failed will only show you the end of the logs...

提示:部署时,请立即查看日志,以便了解整个部署过程中发生的情况。在失败后查看日志只会显示日志的结尾...

Lastly, ensure you listen on the proper port. I do this (as others do too)

最后,确保您监听正确的端口。我这样做(其他人也这样做)

var serverPort = 8080;
. . .
var port = process.env.PORT || serverPort;

回答by Renil Joseph

This may be helpful to someone. My initial Profile content was this :

这可能对某人有帮助。我最初的个人资料内容是这样的:

web : node server.js

and after changing it to this, it worked :

将其更改为此后,它起作用了:

web:node server.js

The space between 'web' and ':' symbol was the problem.

'web' 和 ':' 符号之间的空格是问题所在。

回答by EAzevedo

I just had the same error on my heroku app. I installed cors locally and pushed only the file app.js to the repository and deployed it to heroku. The error was actually that the cors package wasnt declared on my package.json (I only pushed app.js to github) so when heroku tried to call the app it caused an error because cors was undefined.

我的 heroku 应用程序也出现了同样的错误。我在本地安装了 cors,只将文件 app.js 推送到存储库并将其部署到 heroku。错误实际上是我的 package.json 上没有声明 cors 包(我只将 app.js 推送到 github),因此当 heroku 尝试调用该应用程序时,它导致了错误,因为 cors 未定义。

The point is, also check if the package.json is updated.

关键是,还要检查 package.json 是否已更新。

回答by Jaskaran Batra

This Might help others

这可能会帮助其他人

  1. From Terminal Check out your HerokuLogs
  2. Check out the error message
  3. This might be possible that any npm module you have installed globally
  4. Then just install that npm module locally.
  5. Commit the updated package.json and package-lock.json file on git.
  6. Then run push git Herokumaster.
  1. 从终端检查您的Heroku日志
  2. 查看错误信息
  3. 这可能是您全局安装的任何 npm 模块
  4. 然后只需在本地安装该 npm 模块。
  5. 在 git 上提交更新的 package.json 和 package-lock.json 文件。
  6. 然后运行push git Herokumaster。