How to check if a request is http or https in Node.js?

Sometimes, we want to check if a request is http or https in Node.js.

In this article, we’ll look at how to check if a request is http or https in Node.js.

How to check if a request is http or https in Node.js?

To check if a request is http or https in Node.js, we can use the req.secure property in our middlewares.

req.secure is a shorthand for req.protocol === 'https'.

And if our app is behind a proxy, we need to enable 'trust proxy' so that req.protocol reflects the protocol that’s used to communicate between the client and proxy.

To enable it, we write

app.enable('trust proxy');

Conclusion

To check if a request is http or https in Node.js, we can use the req.secure property in our middlewares.