EN
Express.js - add cookie
0
points
In this short article, we would like to add cookie in Express.js.
1. Install cookie-parser
package:
npm install cookie-parser
2. Use response.cookie()
method to add cookies with specified cookie name and value.
Practical example
In this example, we use response.cookie()
method to create username
cookie wich stores john
value.
const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()
app.use(cookieParser())
app.get('/', (request, response){
response.cookie('username', 'john').send('Cookie set'); // username=john
});
app.listen(3000);
After running the code and entering localhost:3000
, we will see the window below.