Heroku的node.js应用程序崩溃了。

7

我尝试通过Heroku部署Node.js。然而,出现了应用程序错误。

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 322 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  v6.8.1
remote:        engines.npm (package.json):   3.10.8
remote:        
remote:        Resolving node version v6.8.1 via semver.io...
remote:        Downloading and installing node 6.8.1...
remote:        npm 3.10.8 already installed with node
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote:        ├── cookie@0.3.1
remote:        ├── ejs@2.5.2
remote:        └── jade@1.11.0
remote:        
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> web
remote: 
remote: -----> Compressing...
remote:        Done: 14.5M
remote: -----> Launching...
remote:        Released v9
remote:        https://sawartc.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/sawartc.git
   7d9216c..973d8ba  master -> master
sawamuratakashikan-no-MacBook-Pro:node sawamuratakahiro$ heroku open

部署可能成功了,

但是出现了应用程序错误。

这是Heroku的日志记录:

2016-10-24T15:25:07.521109+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T15:25:07.500572+00:00 heroku[web.1]: Process exited with status 137
2016-10-24T17:10:24.410630+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T17:10:26.487227+00:00 heroku[web.1]: Starting process with command `npm start`
2016-10-24T17:10:29.973959+00:00 app[web.1]: 
2016-10-24T17:10:29.973971+00:00 app[web.1]: > node@1.0.0 start /app
2016-10-24T17:10:29.973972+00:00 app[web.1]: > node sampleapp.js
2016-10-24T17:10:29.973973+00:00 app[web.1]: 
2016-10-24T17:10:30.163591+00:00 app[web.1]: Server running at http://127.0.0.1:1337/
2016-10-24T17:11:26.793014+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2016-10-24T17:11:26.755681+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-10-24T17:11:26.755825+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-10-24T17:11:26.903907+00:00 heroku[web.1]: Process exited with status 22
2016-10-24T17:11:26.919339+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T20:08:20.748447+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T20:08:22.155076+00:00 heroku[web.1]: Starting process with command `npm start`
2016-10-24T20:08:24.669537+00:00 app[web.1]: > node@1.0.0 start /app
2016-10-24T20:08:24.669514+00:00 app[web.1]: 
2016-10-24T20:08:24.669538+00:00 app[web.1]: > node sampleapp.js
2016-10-24T20:08:24.669539+00:00 app[web.1]: 
2016-10-24T20:08:24.786219+00:00 app[web.1]: Server running at http://127.0.0.1:1337/
2016-10-24T20:09:22.309836+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-10-24T20:09:22.309836+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-10-24T20:09:22.399015+00:00 heroku[web.1]: Process exited with status 137
2016-10-24T20:09:22.406141+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-25T01:40:39.946449+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-25T01:40:41.519699+00:00 heroku[web.1]: Starting process with command `npm start`
2016-10-25T01:40:43.843569+00:00 app[web.1]: 
2016-10-25T01:40:43.843581+00:00 app[web.1]: > node@1.0.0 start /app
2016-10-25T01:40:43.843581+00:00 app[web.1]: > node sampleapp.js
2016-10-25T01:40:43.843582+00:00 app[web.1]: 
2016-10-25T01:40:43.943591+00:00 app[web.1]: Server running at http://127.0.0.1:1337/
2016-10-25T01:41:42.057140+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-10-25T01:41:42.057234+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-10-25T01:41:42.172614+00:00 heroku[web.1]: Process exited with status 137
2016-10-25T01:41:42.207278+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-25T03:42:53.015078+00:00 heroku[api]: Deploy 2e44739 by 
2016-10-25T03:42:53.015138+00:00 heroku[api]: Release v7 created by 
2016-10-25T03:42:53.546204+00:00 heroku[slug-compiler]: Slug compilation finished
2016-10-25T03:42:53.546194+00:00 heroku[slug-compiler]: Slug compilation started
2016-10-25T03:42:53.630591+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-25T03:42:54.901534+00:00 heroku[web.1]: Starting process with command `npm start`
2016-10-25T03:42:57.073592+00:00 app[web.1]: 
2016-10-25T03:42:57.073608+00:00 app[web.1]: > node@1.0.0 start /app
2016-10-25T03:42:57.073608+00:00 app[web.1]: > node sampleapp.js
2016-10-25T03:42:57.192757+00:00 app[web.1]: Server running at http://127.0.0.1:1337/
2016-10-25T03:42:57.073609+00:00 app[web.1]: 
2016-10-25T03:43:55.242924+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-10-25T03:43:55.242924+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-10-25T03:43:55.403793+00:00 heroku[web.1]: Process exited with status 137
2016-10-25T03:43:55.418227+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-25T03:43:55.419140+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-25T03:43:56.641767+00:00 heroku[web.1]: Starting process with command `npm start`
2016-10-25T03:43:58.885658+00:00 app[web.1]: > node@1.0.0 start /app
2016-10-25T03:43:58.885646+00:00 app[web.1]: 
2016-10-25T03:43:58.885659+00:00 app[web.1]: > node sampleapp.js
2016-10-25T03:43:58.885663+00:00 app[web.1]: 
2016-10-25T03:43:58.988687+00:00 app[web.1]: Server running at http://127.0.0.1:1337/
2016-10-25T03:44:20.981992+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=sawartc.herokuapp.com request_id=19ae4ca5-679f-4ef1-9e5c-9408dd6963b2 fwd="219.117.193.211" dyno= connect= service= status=503 bytes=
2016-10-25T03:44:57.129700+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-10-25T03:44:57.129700+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-10-25T03:44:57.244921+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-25T03:44:57.267311+00:00 heroku[web.1]: Process exited with status 137

那么,我应该怎么部署这个node.js应用呢?


当您在本地运行它时会发生什么? - R.A. Lucas
3个回答

17

根据日志显示,您正在绑定到动态端口1337,但这是本地运行的,Heroku只允许使用端口8080。最好的方法是定义您的端口:

var port = process.env.PORT || 1337;

因此,在本地运行代码时,Heroku使用其端口,而您仍然可以使用1337端口。


我也遇到了同样的问题。但是8080端口也无法使用。可能还有其他原因吗? - neha soni
环境是区分大小写的,请确保使用 PORT 而不是 port。永远不要设置 PORT 环境变量,让 Heroku 为您完成它。 - mateusppereira

1

您需要在package.json文件中添加启动脚本,类似于以下内容:

"scripts": {
    "start": "node server.js"
}

0

问题出在默认的“start”脚本上。我认为在免费的dyno上,Heroku只允许运行“npm start”。然而,默认的启动脚本附带了开发服务器。

因此,在您的package.json文件中,将“start”脚本中的命令替换为“serve -s build”,它就可以正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接