2

I'm trying to run a little HTTPS server with Heroku, but it's just giving me this error:

at=error code=H13 desc="Connection closed without response" method=GET path="/" host=###.herokuapp.com request_id=### fwd="###" dyno=web.1 connect=0ms service=6ms status=503 bytes=0 protocol=https

My server looks like this:

let https = require("https");
let port = process.env.PORT;
if (port == null || port == "") {
    console.log("Using port 80");
    port = 8000;
} else {
    console.log("Using port supplied by Heroku");
    port = Number(port);
}

console.log(`Listening on port ${port}`);
const options = {
    key: [my key],
    cert: [my cert]
};
 
https.createServer(options, (request, response) => {
    console.log("Request recieved")
    response.writeHead(200);
    response.write("<!DOCTYPE html><head></head><body>hello world</body>")
    response.end();
}).listen(port);

I dont have any problems running it locally with heroku local web. Why does this happen and how can I fix this?

EDIT: Turns out that you have to pay Heroku 25 bucks a month to get HTTPS. See this answer.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
axhom
  • 21
  • 3
  • I see that you have found an answer to your problem - Great!. You should actually post it as an answer. It may help someone with similar issue in the future – beniutek Dec 31 '20 at 23:46
  • "Turns out that you have to pay Heroku 25 bucks a month to get HTTPS"—not true. You get HTTPS automatically on your Heroku subdomain, and if you want to use a custom domain with HTTPS [that's included as long as you have a paid dyno](https://devcenter.heroku.com/articles/automated-certificate-management), hobby dynos included, which are $7 a month. – ChrisGPT was on strike Dec 31 '20 at 23:50

1 Answers1

0

My server looks like this:

let https = require("https");

You shouldn't be dealing with HTTPS in your application code on Heroku (or probably on other hosting environments, but let's focus on Heroku where you can't).

Instead, just run a regular HTTP server and let Heroku route HTTPS traffic to your application. For Node.js, that usually looks something like this (using Express; simplified from Heroku's getting started with Node.js guide):

const PORT = process.env.PORT || 5000

express()
  .listen(PORT, () => console.log(`Listening on ${ PORT }`))

If you want to redirect HTTP to HTTPS you can install something like express-to-https.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257