heroku-deno-getting-started
heroku-deno-getting-started copied to clipboard
How to get heroku port number?
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
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
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);
I have not had issues deploying with ABC but this can't hurt thanks!
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.
Can we access it in the application as Deno.env.get('PORT')
?
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).