I set cookie using the below code,
app.get("/", (req, res) => {
let options = {
maxAge: 1000 * 60 * 15, // would expire after 15 minutes
httpOnly: true, // The cookie only accessible by the web server
};
let userData = {
name: "John",
age: 23,
};
// Set cookie
res.cookie("cookieName", userData, options); // options is optional
res.send("done");
});
Read the cookie using,
app.get("/get", function (req, res) {
// read cookies
res.send(req.cookies["cookieName"]);
});
I need to update the value in the cookie, need to change age:23 to age:32. How can I update the cookie?
let userData = {
name: "John",
age: 32,
};
res.cookie("cookieName", userData, options); // options is optional
same code will work for update part, if key name is same, if key name is different it'll create new key in cookie