Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In express.js req.protocol is not picking up "https" for my secure link. It always pics "http"

req.protocol is not picking up "https" for my secure link. It always pics "http" . For creating my baseURL am using this. any solutions please?

like image 597
Sreekanth P M Avatar asked Nov 07 '16 07:11

Sreekanth P M


People also ask

Does express JS support HTTPS?

Enable HTTPS in Expressjs and your server should be available at address https://localhost:3000 . Please be aware that browsers reject self-signed certificates by default, so when you open https://localhost:3000 for the first time, you'll see a browser warning instead of an expected page.

How do I change from HTTP to HTTPS in Express?

The process in detail. We will perform HTTP to HTTPS redirection by creating an Express middleware function [ 1] and then, inside that function, write the redirection code that will force Express to use HTTPS. The middleware function provides access to the Express req and res objects and next function that we will need ...

Does Express need HTTP?

There's really no reason to create your own http server using the http module. Express will just do that for you with app. listen() just fine and save you little bit of typing. If you were creating an https server, then you would need to use the https module and pass security credentials to https.

Which of the following is the default encoding in Express JS?

Parameters: The default encoding is 'utf8' and the data parameter is basically the data with which the user wants to end the response. Return Value: It returns on Object.


1 Answers

That's happening most probably because there is a proxy in between. In my case I am using Heroku.

If that's the case, you need to add app.enable('trust proxy'); to your express app.

Without enabling that, req.protocol returned http. After that change I got https.

You can also use req.secure to get true or false depending on if your app is secured by https or not.

like image 197
Martin Schaer Avatar answered Sep 19 '22 10:09

Martin Schaer