Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *