Node.js: Simple app not working on Heroku

2019-02-02 17:25发布

I've written a basic node.js application and I've managed to deploy it on Heroku without having any problem. I've created my package.json and Procfile, however from logs I see that there is no running processes, thus cannot get any response. What could be the problem?

PS: I do not want to use the Express framework

My Code:

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

My package.json:

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

Logs:

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=

4条回答
在下西门庆
2楼-- · 2019-02-02 17:36

Things I had to do (didn't have to scale or use Express)..

  1. Create a Procfile in the root directory (a file literally called Procfile, no extension).
    Inside Procfile, type:

    web: node server.js
    
  2. Add a script and engine to package.json

    "scripts": {
            "start": "node server.js"
    }
    
    "engines": {
            "node": "8.9.3"
    }
    
  3. Update port in server.js

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

And why..

  1. To explicitly declare what command should be executed to start your app. Heroku looks for a Procfile specifying your process types

  2. For the script, if no Procfile is present in the root directory during the build process, your web process will be started by running npm start. For the engine, to specify a Node version that matches the runtime you’re developing with and want to use on Heroku.

  3. Heroku already assigns your app a port and adds it to the env, so you can't set the port to a fixed number.

Resources:

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

查看更多
虎瘦雄心在
3楼-- · 2019-02-02 17:47

Have you scaled the heroku app?

$ heroku ps:scale web=1

This is a required step. The 1 is the number of processes you want spawned for your app.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-02 17:47

Change your port

from

.listen(8888)

to

.listen(process.env.PORT || 8888)
查看更多
神经病院院长
5楼-- · 2019-02-02 17:53

What's inside your Procfile? Does it match your app name?

$ ls
app.js Procfile
$ cat Procfile
web: node app$
$
查看更多
登录 后发表回答