Express.js req.secure Property
Last Updated :
17 Mar, 2023
The req.secure property is a Boolean property that is true if a TLS connection is established else returns false.
Syntax:
req.secure
Return Value: It returns a Boolean value either True or False.
Installation of the express module:
You can visit the link to Install the express module. You can install this package by using this command.
npm install express
After installing the express module, you can check your express version in the command prompt using the command.
npm version express
After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js
Project Structure:
Example 1: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
console.log(req.secure);
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Make sure you have installed the express module using the following command:
npm install express
Run the index.js file using the below command:
node index.js
Output:
Console Output:
Server listening on PORT 3000
Browser Output:
Now open your browser and go to http://localhost:3000/, now you can see the following output on your console:
Server listening on PORT 3000
false
Example 2: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
if (req.secure) {
console.log("Secured Connection")
res.end();
} else {
console.log("Less Secured Connection");
res.end();
}
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the application:
Run the index.js file using the below command:
node index.js
Output: Now open your browser and make a GET request to http://localhost:3000, now you can see the following output on your console:
Server listening on PORT 3000
Less Secured Connection
Reference: https://expressjs.com/en/4x/api.html#req.secure
Similar Reads
Express.js req.protocol Property The req.protocol property contains the request protocol string which is either HTTP or (for TLS requests) https. When the trust proxy setting does not evaluate to false, this property will use the X-Forwarded-Proto header field value if it is present. Syntax: req.protocol Parameter: No parameters.
2 min read
Express res.locals Property The `res.locals` property is an object that holds response local variables specific to the current request. It has a scope limited to the request and is accessible only to the view(s) rendered during that particular request/response cycle, if any. Syntax:res.localsParameter: No parameters. Return Va
2 min read
Express req.query Property The req.query property in Express allows you to access the query parameters from the URL of an incoming HTTP request. Query parameters are typically key-value pairs that are appended to the URL after the "?" symbol, and they are separated by the "&" symbol.Syntax:req.queryParameter: req.query do
2 min read
HTTP Request and Response Cycle in Express.js The HTTP request and response cycle is the backbone of secure communication on the web. In Express.js, req (request) and res (response) objects are fundamental to handling HTTP requests and sending back responses. They are passed as arguments to route handlers and middleware functions. In this artic
5 min read
Authentication strategies available in Express Authentication is an important aspect of web development, which ensures that users accessing an application are who they claim to be. In Express, several authentication strategies are available that help you secure your applications by verifying user identities. In this article, we will cover the fo
5 min read
How to check user authentication in GET method using Node.js ? There are so many authentication methods like web token authentication, cookies based authentication, and many more. In this article, we will discuss one of the simplest authentication methods using express.js during handling clients get a request in node.js with the help of the HTTP headers. Appro
3 min read