npm cookie-parser
- Install
cookie-parser
package using npm:
npm install cookie-parser
- Require
cookie-parser
in your Node.js application:
const cookieParser = require('cookie-parser');
- Use
cookie-parser
middleware in your Express application:
app.use(cookieParser());
- Access cookies in your route handlers or middleware:
app.get('/example', (req, res) => {
// Access cookies using req.cookies object
const myCookie = req.cookies.myCookie;
res.send(`Value of myCookie: ${myCookie}`);
});
- Set cookies in your route handlers or middleware:
app.get('/set-cookie', (req, res) => {
// Set a cookie using res.cookie method
res.cookie('myCookie', 'cookieValue');
res.send('Cookie set successfully');
});
- Configure options for
cookie-parser
:
// Example with options
app.use(cookieParser({
// Options go here
}));
Note: Refer to the official documentation for cookie-parser
for more details on available options and usage: https://www.npmjs.com/package/cookie-parser