Scenario:
You are building a web application using the benhall/express-demo project, and you need to manage cookies in your Express application. Cookies are small pieces of data that can be stored on the client-side by the browser and sent back to the server with each request. They are commonly used for session management, authentication, and storing user preferences.
Solution:
To implement cookies in your Express application built with benhall/express-demo, you will use a cookie middleware, configure the cookie options, and handle cookie data.
Step 1: Install the cookie-parser middleware
First, you need to install the cookie-parser
middleware, which is used to parse cookie headers into a JavaScript object. You can install it using npm by running the following command in your project directory:
npm install cookie-parser
Step 2: Import and use the cookie-parser middleware
Next, import the cookie-parser
middleware in your app.js
file and use it as middleware:
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
Step 3: Set cookies
To set cookies, you can use the res.cookie()
method provided by the cookie-parser
middleware. Here’s an example of setting a cookie named sessionId
with a value of abc123
and a max age of one hour:
app.get('/set-cookie', (req, res) => {
res.cookie('sessionId', 'abc123', { maxAge: 3600000 });
res.send('Cookie set');
});
Step 4: Read cookies
To read cookies, you can access them as properties of the req
object:
app.get('/read-cookie', (req, res) => {
const sessionId = req.cookies.sessionId;
res.send(`Cookie value: ${sessionId}`);
});
Step 5: Delete cookies
To delete cookies, you can set the maxAge
property to a negative value:
app.get('/delete-cookie', (req, res) => {
res.cookie('sessionId', '', { maxAge: -1 });
res.send('Cookie deleted');
});
Tests:
To verify the implementation of cookies in your Express application, you can write tests using a testing framework like Mocha and Chai. Here’s an example of a test file that checks if cookies are set and read correctly:
const request = require('supertest')(app);
const assert = require('assert');
describe('Cookies', () => {
it('should set and read a cookie', async () => {
const res = await request.get('/set-cookie');
assert.strictEqual(res.text, 'Cookie set');
const res2 = await request.get('/read-cookie');
assert.notStrictEqual(res2.text, 'Cookie set');
assert.strictEqual(res2.text, `Cookie value: abc123`);
});
it('should delete a cookie', async () => {
await request.get('/set-cookie');
const res = await request.get('/read-cookie');
assert.strictEqual(res.text, `Cookie value: abc123`);
await request.get('/delete-cookie');
const res2 = await request.get('/read-cookie');
assert.strictEqual(res2.text, 'Cookie deleted');
});
});
Make sure to run your tests using a testing framework like Mocha and Chai to ensure that your implementation of cookies in your Express application is working correctly.