Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express.js – req.signedCookies Property
req.signedCookies contains the signed cookies (ready for use) sent by the request while using the cookie-parser middleware. Signed cookies exist in a different object to show the developer intent, else a malicious attack could be made on the request.cookie values that are relatively easy to spoof.
The property defaults are set to "{ }", if no cookies are sent.
Syntax
req.signedCookies
Installing the cookie-parser module
npm install cookie-parser
Example 1
Create a file with the name "reqSignedCookies.js" and copy the following code snippet. After creating the file, use the command "node reqSignedCookies.js" to run this code as shown in the example below −
// req.signedCookies() Method Demo Example
// Importing the express & cookieParser module
var cookieParser = require('cookie-parser');
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining the cookieParser to be used
app.use(cookieParser());
// Defining an api endpoint
app.get('/api', function (req, res) {
req.signedCookies.title='TutorialsPoint';
console.log(req.signedCookies);
res.send();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
Hit the following Endpoint with a GET request: localhost:3000/api
C:\home
ode>> node reqSignedCookies.js Server listening on PORT 3000 [Object: null prototype] { title: 'TutorialsPoint' }
Example 2
Let's take a look at one more example.
// req.signedCookies() Method Demo Example
// Importing the express & cookieParser module
var cookieParser = require('cookie-parser');
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining the cookieParser to be used
app.use(cookieParser());
// Defining an api endpoint
app.get('/api', function (req, res) {
// Setting multiple cookies
req.signedCookies.title='Mayank';
req.signedCookies.age=21;
console.log(req.signedCookies);
res.send();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
Hit the following Endpoint with a GET Request: localhost:3000/api
C:\home
ode>> node reqSignedCookies.js Server listening on PORT 3000 [Object: null prototype] { title: 'Mayank', age: 21 }
Advertisements