interestingMen
2022-03-24 17:19
采纳率: 100%
浏览 44

node.js服务端设置cookie,设置成功却拿不到设置的cookie数据(cookie-parser)

**node.js服务端设置cookie,设置成功却拿不到设置的cookie数据(cookie-parser)**

router.get('/login', function (req, res) {
  res.cookie("USER_ID", "abc", {
    // domain: "*", // 设置生效域名/
    httpOnly: true, // 只运行Http访问
    maxAge: 1000 * 60 * 60 * 2, // 2个小时过期
  });
  res.send({
    success: true,
    info: "登录成功",
  });
})
router.get('/logout', function (req, res) {
  res.cookie("USER_ID", "abc", {
    // domain: "*",
    maxAge: -1, // 过期
    httpOnly: true,
  });
  res.send({
    success: true,
    info: "登出成功",
  });
})
router.get('/test', async function (req, res) {
  console.log(req.cookies,'c');
  if (req.cookies.USER_ID) {
    res.send({
      success: true,
      info: `用户【${req.cookies.USER_ID}】已登录`,
      cookie: req.cookies.USER_ID,
    });
  } else {
    res.send({
      success: false,
      info: "用户未登录",
    });
  }
})

img

4条回答 默认 最新

相关推荐 更多相似问题