Node.js的:简单的应用程序不工作在Heroku(Node.js: Simple app not

2019-07-01 17:25发布

我写了一个基本的Node.js应用程序,我已经成功,而无需任何问题在Heroku上部署它。 我已经建立了我的package.json和Procfile,但是从日志中我看到,有没有正在运行的进程,从而无法得到任何回应。 可能是什么问题呢?

PS:我不想使用Express框架

我的代码:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();

  console.log("I am working");
}).listen(8888);

我的package.json:

{
  "name": "node-example",
  "version": "0.0.1",
  "dependencies": {
  },
  "engines": {
    "node": "0.8.x",
    "npm": "1.1.x"
  }
}

日志:

2012-10-22T12:36:58+00:00 heroku[slugc]: Slug compilation started
2012-10-22T12:37:07+00:00 heroku[slugc]: Slug compilation finished
2012-10-22T12:40:55+00:00 heroku[router]: Error H14 (No web processes running) -> GET aqueous-bastion-6914.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=
2012-10-22T12:50:44+00:00 heroku[router]: Error H14 (No web processes running) -> GET aqueous-bastion-6914.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=

Answer 1:

你攀登Heroku的应用程序?

$ heroku ps:scale web=1

这是一个必要步骤。 该1是你想催生了您的应用程序的进程数。



Answer 2:

更改端口

.listen(8888)

.listen(process.env.PORT || 8888)


Answer 3:

什么是你的Procfile里面? 它是否符合你的应用程序的名字吗?

$ ls
app.js Procfile
$ cat Procfile
web: node app$
$


Answer 4:

事情我不得不这样做(没有缩放或使用快递)..

  1. 创建在根目录下的Procfile(一个字面上叫Procfile文件,没有扩展名)。
    里面Procfile,类型:

     web: node server.js 
  2. 添加脚本引擎的package.json

     "scripts": { "start": "node server.js" } "engines": { "node": "8.9.3" } 
  3. 在server.js更新端口

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

为什么..

  1. 要明确声明要执行,开始您的应用程序是什么命令。 Heroku的寻找一个Procfile指定您的处理类型

  2. 对于脚本,如果没有Procfile出现在生成过程中的根目录下,你的web程序将运行NPM开始启动。 对于发动机来说,要指定你与发展相匹配的运行时节点版本,并希望在Heroku上使用。

  3. Heroku的已分配您的应用程序的端口,并把它添加到ENV,所以你不能端口设置为一个固定的数字。

资源:

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

https://devcenter.heroku.com/articles/troubleshooting-node-deploys

https://devcenter.heroku.com/articles/deploying-nodejs



文章来源: Node.js: Simple app not working on Heroku