在Heroku上部署Node.js应用程序

时间:2015-12-28 11:46:44

标签: node.js heroku

我是初学者。我试着部署一个" app"在Heroku上,但去https://peaceful-coast-7293.herokuapp.com/时它不起作用。它适用于heroku local。 真的不知道还有什么可以检查。

这就是我在日志中所得到的:

2015-12-28T10:56:44.862230+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=peaceful-coast-7293.herokuapp.com request_id=c0994f20-ef28-4cad-95ba-f861a89d698d fwd="93.33.20.213" dyno= connect= service= status=503 bytes=
2015-12-28T10:56:45.457317+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=peaceful-coast-7293.herokuapp.com request_id=a3286f8e-7051-419d-9e3d-ccb2090659c0 fwd="93.33.20.213" dyno= connect= service= status=503 bytes=

我也得到那些npm:

2015-12-28T11:21:07.264470+00:00 heroku[web.1]: State changed from crashed to starting
2015-12-28T11:21:08.151361+00:00 heroku[web.1]: Starting process with command `npm start`
2015-12-28T11:21:10.533548+00:00 app[web.1]: npm ERR! Linux 3.13.0-71-generic
2015-12-28T11:21:10.534886+00:00 app[web.1]: npm ERR! node v0.12.7
2015-12-28T11:21:10.534398+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2015-12-28T11:21:10.535773+00:00 app[web.1]: npm ERR! npm  v2.11.3
2015-12-28T11:21:10.536011+00:00 app[web.1]:
2015-12-28T11:21:10.536325+00:00 app[web.1]: npm ERR! missing script: start
2015-12-28T11:21:10.536603+00:00 app[web.1]: npm ERR!
2015-12-28T11:21:10.536829+00:00 app[web.1]: npm ERR! If you need help, you may report this error at:
2015-12-28T11:21:10.537055+00:00 app[web.1]: npm ERR!     <https://github.com/npm/npm/issues>
2015-12-28T11:21:10.544211+00:00 app[web.1]:
2015-12-28T11:21:10.544631+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2015-12-28T11:21:10.544950+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2015-12-28T11:21:11.360279+00:00 heroku[web.1]: State changed from starting to crashed
2015-12-28T11:21:11.348180+00:00 heroku[web.1]: Process exited with status 1
2015-12-28T11:35:00.705391+00:00 heroku[api]: Starting process with command `node console` by alessio.breviglieri@gmail.com
2015-12-28T11:35:02.612018+00:00 heroku[run.1521]: Awaiting client
2015-12-28T11:35:02.882034+00:00 heroku[run.1521]: State changed from starting to up
2015-12-28T11:35:32.616428+00:00 heroku[run.1521]: Error R13 (Attach error) -> Failed to attach to process
2015-12-28T11:35:33.493974+00:00 heroku[run.1521]: Process exited with status 128
2015-12-28T11:35:33.493559+00:00 heroku[run.1521]: State changed from up to complete

我的app.js文件:

var router = require('./router.js');
var port = process.env.PORT || 3000;

//Create a web server
var http = require('http');
http.createServer(function (request, response) {
  router.home(request, response);
}).listen(port);
console.log('Server running');

Procfile

web: node app.js

package.jsom

{
  "name": "lorem_ipsum",
  "version": "1.0.0",
  "description": "Lorem Ipsum Generator",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Alessio",
  "license": "MIT",
  "keywords": [
    "lorem",
    "ipsum"
  ],
  "engines": {
    "node": "0.12.7"
  }
}

2 个答案:

答案 0 :(得分:2)

您需要添加

"start": "node app.js"

"scripts"的{​​{1}}部分。

答案 1 :(得分:0)

您必须通知heroku从哪里开始:missing script: start。在您的package.json中,

您应该具有以下内容:

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

所以您的package.json将像这样更新,

{
  "name": "lorem_ipsum",
  "version": "1.0.0",
  "description": "Lorem Ipsum Generator",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Alessio",
  "license": "MIT",
  "keywords": [
    "lorem",
    "ipsum"
  ],
  "engines": {
    "node": "0.12.7"
  }
}

其中app.js是您的入口点。

或者,您可以在Procfile中指定:

web: node app.js

但是正如您提到的,Procfile无法正常工作。因此,请尝试使用脚本的start属性。