node在服务端设置多个cookies?

方案一:

缺点:不能设置过期时间,和domain

  res.setHeader('Set-Cookie', [ `uid=${uid}`,  accessToken=${accessToken}`,`sdktoken=${sdktoken}`]);

方案二:

缺点:只能设置一个,最后一个才生效,uid注入不了

import cookie from 'cookie'

 function setCookies(key,value){
      if(!value){
          return;
      }
      res.setHeader('Set-Cookie', cookie.serialize(key, String(value), {
        httpOnly: true,
        maxAge: 60 * 60 * 24 * 7, // 1 week
        domain:'.xx.com'
      }));
    }

  setCookies('uid','11');
 
   setCookies('accessToken','22');
阅读 225
评论
    1 个回答

    这样即可

      const expiredays = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 29;
        const exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
    
        const expiresValue=`;expires=${exdate.toUTCString()};domain=.xx.com;path=/`;
    
        res.setHeader('Set-Cookie', [ `uid=${uid}${expiresValue}`,  `accessToken=${accessToken}${expiresValue}`,`sdktoken=${sdktoken}${expiresValue}`]);
       
      撰写回答

      登录后参与交流、获取后续更新提醒

      相似问题
      推荐文章