백엔드/NodeJs
[NodeJs] 쿠키 읽기 / 쓰기 (cookie read / write) [ javascript / http / client / server]
jinwanseo
2021. 3. 31. 23:01
728x90
NodeJs 쿠키 읽기 / 쓰기 (cookie read / write)

쿠키란 ?
서버가 클라이언트의 웹브라우저에 전송하는 데이터.
브라우저에서는 해당 데이터를 동일 서버에 재접속시 다시 전송한다.
사용 예로는 로그인 상태 유지, 장바구니 상품 저장 등
세션 관리, 개인화, 트래킹 등 다양하게 활용이 가능하다.
클라이언트 측으로 쿠키 쓰기
const express = require('exrpess');
const app = express();
//서버 -> 클라이언트 쿠키값 보내기
//'쿠키의키값=값' => 'tistory=goodmemory'
app.get('/',(req,res)=>{
//쿠키 보내기
res.writeHead(200,{'set-cookie':'tistory=goodmemory'});
res.end();
});
클라이언트 측에서 전송 받은 쿠키 읽기
const express = require('exrpess');
const app = express();
//클라이언트 -> 서버로 재전송한 쿠키값 읽기
app.get('/',(req,res)=>{
console.log(req.headers.cookie);
res.end();
});
728x90