Derek Bredensteiner Derek Bredensteiner - 8 months ago 113
Node.js Question

Heroku NodeJS http to https ssl forced redirect

I have an application up and running on heroku with express on node with https,. How do I identify the protocol to force a redirect to https with nodejs on heroku?

My app is just a simple http server, it doesn't (yet) realize heroku is sending it https requests:

/* Heroku provides the port they want you on in this environment variable (hint: it's not 80) */
app.listen(process.env.PORT || 3000);


The answer is to use the header of 'x-forwarded-proto' that Heroku passes forward as it does it's proxy thingamabob. (side note: They pass several other x- variables too that may be handy, check them out).

My code:

/* At the top, with other redirect methods before other routes */
    next() /* Continue to other routes if we're not redirecting */

Thanks Brandon, was just waiting for that 6 hour delay thing that wouldn't let me answer my own question.