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?
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.
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 ...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With