简体   繁体   English

如何使用 nodejs 更新 cookie

[英]how to update a cookie using nodejs

I set cookie using the below code,我使用下面的代码设置cookie,

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,使用读取 cookie,

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.我需要更新 cookie 中的值,需要将 age:23 更改为 age:32。 How can I update the cookie?如何更新 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相同的代码将适用于更新部分,如果键名相同,如果键名不同,它将在 cookie 中创建新键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM