Set Client-Side Accessible Cookie In Express

后端 未结 3 1371
借酒劲吻你
借酒劲吻你 2021-01-30 21:56

I\'m working on a Node app that uses Express and SocketIO. I want to set a cookie in my Express controller which is then accessible from my client-side Javascript code. Everythi

3条回答
  •  余生分开走
    2021-01-30 22:38

    Actually I have experienced the same issue for couple of hours.

    Here is my code:

    res.cookie("mycookie", "1234567890", { secure:true, maxAge:120000, httpOnly: true });
    

    I can see the Set-Cookie instruction in response header, but in Chrome I can not find the cookie and I can not find the cookie by req.cookies['mycookie'].

    The root cause of this problem is that I did not use HTTPS connection. (Express 4.x with cookie-parser middleware)

    According to this document: Simple Steps to Secure Your Express Node App

    If I set the option secure=true, then the browser will not send my cookie in any HTTP request but HTTPS secure connection. Then after I removed secure:true option, I got my cookie work.

提交回复
热议问题