[Node.js] 세션 쿠키, 영구 쿠키

0. 참고자료

Session 쿠키 VS Permanent 쿠키 – 생활코딩

쿠키와 document.cookie


1. 쿠키의 종류

  • 세션 쿠키 (Session cookie)
    : 웹브라우저를 끄면 사라지는 휘발성 쿠키
  • 영구 쿠키 (Permanent cookie)
    : 웹브라우저를 꺼도 사라지지 않는 쿠키

2. 세션 쿠키

세션 쿠키를 만들기 위해서 별도로 해줄 건 없다.

expires(유효 일자)나 max-age(만료 기간) 옵션이 지정되어있지 않으면, 브라우저가 닫힐 때 쿠키도 함께 삭제된다.

response.writeHead(200, {
    'Set-Cookie':[
        `Session=sessionCookie`
    ]
});
response.end('Cookie!!');
Code language: JavaScript (javascript)

3. 영구 쿠키

세션 쿠키에 Max-AgeExpires 같은 옵션을 추가하면 영구 쿠키가 된다.

  • Max-Age : 얼마 동안 유효한지 설정 (상대적) (초단위)
  • Expires : 언제까지 유효한지 설정 (절대적) (GMT 포맷)

가. Max-Age

let date = 'Tue, 19 Jan 2038 03:14:07 GMT';

...
response.writeHead(200, {
    'Set-Cookie':[
        `Permanent=MaxAgeCookie; Max-Age=${60*60*24}`
    ]
});
response.end('Cookie!!');
Code language: JavaScript (javascript)

하루동안 유효한 쿠키다.

나. Expires

let date = 'Tue, 19 Jan 2038 03:14:07 GMT';

...
response.writeHead(200, {
    'Set-Cookie':[
        `Permanent=expiresCookie; Expires=${date}`
    ]
});
response.end('Cookie!!');
Code language: JavaScript (javascript)

정해진 일자까지 유효한 쿠키다.


댓글 남기기