heroku-deno-getting-started icon indicating copy to clipboard operation
heroku-deno-getting-started copied to clipboard

How to get heroku port number?

Open far11ven opened this issue 5 years ago • 6 comments

How to get heroku port number?

How to specify on which this app runs on?

Like in folowing example present on https://deno.land

import { serve } from "https://deno.land/[email protected]/http/server.ts"; const s = serve({ port: 8000 }); console.log("http://localhost:8000/"); for await (const req of s) { req.respond({ body: "Hello World\n" }); }

In NodeJs we had something like below:

.listen(process.env.PORT || 5000); what;s in deno?

2020-02-15T14:23:46.284551+00:00 heroku[web.1]: Starting process with command deno -A --allow-net=:8000 index.ts 2020-02-15T14:23:45.139667+00:00 app[api]: Release v5 created by user [email protected] 2020-02-15T14:23:45.000000+00:00 app[api]: Build succeeded 2020-02-15T14:23:47.827365+00:00 app[web.1]: Compile file:///app/index.ts 2020-02-15T14:23:51.050050+00:00 app[web.1]: Listening on http://localhost:8000/ 2020-02-15T14:24:46.783798+00:00 heroku[web.1]: State changed from starting to crashed 2020-02-15T14:24:46.705630+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2020-02-15T14:24:46.705723+00:00 heroku[web.1]: Stopping process with SIGKILL 2020-02-15T14:24:46.768561+00:00 heroku[web.1]: Process exited with status 137 2020-02-15T14:24:49.248590+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=deno-http-server.herokuapp.com request_id=546fb246-b75e-4f4b-b47f-6a32eeee3095 fwd="103.251.211.59" dyno= connect= service= status=503 bytes= protocol=https

far11ven avatar Feb 15 '20 14:02 far11ven

If you haven't figured this out yet, you set it in the Procfile: web: deno run --allow-net --allow-read --allow-env --cached-only main.ts --port=${PORT}

Then you call it with Deno: const PORT = parseInt(Deno.env.toObject().PORT) || 8080

tedrand avatar May 23 '20 12:05 tedrand

Please notice, that you need to provide the hostname value for the server, and it should be 0.0.0.0, otherwise you'll get the same R10 (Boot timeout) error.

As an example, here's how I manage this (I am using Pogo):

const options: ServerOptions = {
  port,
};
if (ENV === 'heroku') {
  options.hostname = '0.0.0.0';
}

const server: Server = pogo.server(options);

peterdee avatar May 24 '20 22:05 peterdee

I have not had issues deploying with ABC but this can't hurt thanks!

tedrand avatar May 25 '20 01:05 tedrand

Pogo's default hostname is localhost for security reasons. You really shouldn't be exposing a server to the public without explicitly opting into it. The 0.0.0.0 hostname, which Deno (and other frameworks) use by default is convenient but insecure, because it listens on all available addresses. Heroku's architecture, unfortunately, requires to listen on a public hostname, hence why 0.0.0.0 is needed on Heroku. There are other hosting providers, such as Now, where localhost actually works fine because they do some kind of tunneling to forward localhost to the public network in a controlled manner. I hope Heroku eventually adopts this approach, but for now, Heroku needs 0.0.0.0, and you need to explicitly set that when using Pogo.

I really want Pogo to be the first production-grade Deno web framework and security is a big part of that.

sholladay avatar May 25 '20 17:05 sholladay

Can we access it in the application as Deno.env.get('PORT')?

omirobarcelo avatar Jun 04 '20 15:06 omirobarcelo

Yes you could read it as an environment variable like that, however then your app will require the --allow-env permission, which is a hassle. I would say it's better to read the port number as a command line flag (see main.ts in this repo).

sholladay avatar Jun 04 '20 18:06 sholladay