-
[Express] cookie에 대하여programing/Language 2019. 9. 6. 14:00
보통 Express에서 쿠키를 설정하기 위해서는
res.cookie('key', 'value', options)
를 이용한다.주요 옵션들
httponly
true : http에서만 쿠키를 사용하여, js에서 document.cookie로 접근할 수 없다.
false: js에서도 document.cookie로 접근할 수 있다.
secure
true : https에서만 쿠키가 유효하다,
false : http에서도 쿠기가 유효하다.
overwrite
true : 이전 쿠키를 덮어쓴다.
false : 이전 쿠키를 덮어쓰지 않는다. (즉, 새 쿠키는 증발함)
domain
default : /
쿠키의 도메인을 지정한다. domain과 path헤더는 쿠키의 스코프를 정의합니다.
따라서 왠만하면 도메인을 서버 도메인으로 설정합시다.
주의점
client
요청을 보낼 때,
xhr.withCredentials = true;
를 설정한다.fetch API를 사용한다면,
fetch('특정url', { method: '메소드', ..., credentials: 'include'})
와 같이 credentails옵션을 include로 설정해야 한다.만약 httponly가 false인 경우, 응답을 받은 후,
document.cookie
로 쿠키를 얻는다. true인 경우, postman이나 chrome 개발자도구로확인한다. (크롬 개발자도구는 내부 값을 볼 수 없다.)server
응답을 보낼 때,
res.set({'Access-Control-Allow-Credentials': true})
를 설정한다. (동일 출처가 아닌 경우)쿠키 업데이트가 안되는 경우
res.cookie('key', 'value', options)
에서, options 객체에overwrite: true
옵션을 준다.참고
How to handle cookies in Express JS? - Arjun
Cookies are very useful to store small piece of web application data and cookies are stored on the user’s computer by the user’s web browser while the user is browsing. Express uses the same methods, Cookies, as most other web frameworks to track sessions.
arjunphp.com
'programing > Language' 카테고리의 다른 글
[Node] npx sequelize-cli 사용시 missing ) 에러 (0) 2019.10.16 [JS] vanillaJS repository 모음 (0) 2019.09.07 [Functional] 동시성을 가진 C.reduce, C.take, C.map, C.filter (0) 2019.08.21 [Functional] promise와 monad, kleisli composition (0) 2019.08.15 [Functional] join, find, flatten (0) 2019.08.12 댓글