์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- KDT
- Python
- ๊ฐ๋ฐ
- ํ์ด์ฌ
- mongodb
- CSS
- ํ ์ดํ๋ก์ ํธ
- react
- javascript
- ๋ฐฑ์ค
- ์ฝ๋ฉ
- ํ๋ก ํธ์๋
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ ธ๋ง๋์ฝ๋
- ๊ตญ๋น์ง์
- fe
- ๊ทธ๋ฆฌ๋
- error
- node.js
- Til
- ํฌ๋กค๋ง
- HTML
- ์ฝ๋ฉ์ ํ
- heapq
- ๋๋ฆผ์ฝ๋ฉ
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉํ ์คํธ
- ๋ชจ๊ฐ์ฝ
- ํ๋ก์ ํธ
- JS
- Today
- Total
๐ฑ โ ๐ณ
[node.js] Cookie - ํ์ ์ฐฝ ํ๋ฃจ๋์ ๋ณด์ง ์๊ธฐ ๋ณธ๋ฌธ
๐ Cookie
: HTTP ์ฟ ํค๋ ์๋ฒ๊ฐ ์ฌ์ฉ์์ ์น ๋ธ๋ผ์ฐ์ ์ ์ ์กํ์ฌ ์ ์ฅํ๋ ์์ ๋ฐ์ดํฐ ์ ์ฅ์
- JS์ ๊ฐ์ฒด์ฒ๋ผ key ๊ฐ๊ณผ, data๊ฐ ๋ค์ด ์์
- ๋ธ๋ผ์ฐ์ ๋ ์ฟ ํค๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํด ๋์๋ค๊ฐ, ๋์ผํ ์๋ฒ์ ๋ค์ ์์ฒญ์ ๋ณด๋ด๊ฒ ๋ ๋ ์ฟ ํค์ ์ ์ฅ๋ ์ ๋ณด๋ฅผ ํจ๊ป ์ ์ก
- ์๋ฒ์์๋ ์ฟ ํค๋ฅผ ํตํด ์์ฒญ์ด ์ด๋ค ๋ธ๋ผ์ฐ์ ์ ๋ค์ด์๋์ง(ํน์ ์์ฒญ์ด ๋์ผํ ๋ธ๋ผ์ฐ์ ์์ ๋ค์ด์๋์ง) ๋ฑ์ ํ์ ํ ์ ์์
- ์๋ฒ์ ์ ์ฅํด์ผ ํ ์ ๋ณด๋ฅผ ๊ด๋ฆฌ -> ๋ก๊ทธ์ธ ์ ๋ณด, ์ฅ๋ฐ๊ตฌ๋, ํ์ ํ๋ฃจ ๋์ ์ ๋ณด๊ธฐ ๋ฑ
- ์ค์ํ ์ ๋ณด(์ด๋ฆ, ๋ฐ์ดํฐ, ๋ง๋ฃ์ผ, ๊ฒฝ๋ก ๋ฑ)๋ ๋ก์ปฌ์ ๋จ๊ฒ ๋๋ฏ๋ก ์ ๋ ์ ์ฅํด์๋ ์๋จ
โ Cookie์ ๋์ ๋ฐฉ์


โ ํ๋ก ํธ์์ ์ฟ ํค ์ฌ์ฉํ๊ธฐ
(์ฌ์ฉ X, ํธ๋ฒ)
document.cookie๋ก ์ฌ์ฉ
<script>
console.log(document.cookie);
document.cookie = "user=test1; expires=19 Sep 2022 13:00:00 GMT; path=/";
document.cookie = "test=test2; expires=19 Sep 2022 13:00:00 GMT; path=/";
console.log(document.cookie);
</script>
expires๊ฐ๊ณผ ๋ธ๋ผ์ฐ์ ์ ์๊ฐ์ ๋น๊ตํด์ ์ฟ ํค๊ฐ ํด๋น ์๊ฐ์ด ๋๋ฉด ์๋์ผ๋ก ์ญ์ ์ฒ๋ฆฌ
window.onload = () => {
if (document.cookie) alert(`์ฟ ํค ํ์์! ${document.cookie}`);
};
Front JS ์ฝ๋๋ฅผ ํตํด ํน์ ์ํฉ์์ ํ์ด์ง๊ฐ ์๋ก ๊ณ ์นจ ํด๋ ์ ์ฅ์ด ํ์ํ ์ ๋ณด๋ฅผ ์ฟ ํค์ ์ ์ฅํ์ฌ DB์ฒ๋ผ ํ์ฉ๋ ๊ฐ๋ฅ
(ํ์ง๋ง, ๋ฑํ ์ธ ์ผ์ ๋ง์ง ์์)
๐ ๋ธ๋ผ์ฐ์ ์์ ์ฟ ํค ํ์ธํ๊ธฐ
๊ฐ๋ฐ์ ๋๊ตฌ -> Application -> Storage -> Cookies ์ ๊ฐ๋ฉด ์ฟ ํค ์ ๋ณด ํ์ธ ๊ฐ๋ฅ

โ ๋ฐฑ์๋(express)์์ ์ฟ ํค ์ฌ์ฉํ๊ธฐ
cookie-parsser ๋ชจ๋ ์ฌ์ฉ
npm i cookie-parder -s
๋ฉ์ธ ์๋ฒ์ ์ฟ ํค ๋ชจ๋ ํธ์ถ ๋ฐ ๋ฏธ๋ค์จ์ด ๋ฑ๋ก
const cookieParser = require('cookie-parser');
app.use(cookieParser());
์ฟ ํค๋ฅผ ์ฌ์ฉํ ๊ณณ์์ res.cookie('์ฟ ํค ์ด๋ฆ', '๋ฐ์ดํฐ', '์ต์ ๊ฐ์ฒด'); ๋ฅผ ์จ์ ์ฌ์ฉ
res.cookie('alert', true, {
expires: new Date(Date.now() + 1000 * 60),
httpOnly: true,
});
๐ ์ต์ ์ ์๋ฏธ
- expires: ์ฟ ํค๊ฐ ์๋์ผ๋ก ์ญ์ ๋๋ ์ผ์ ์ง์
- httpOnly: ํด๋น ์ฟ ํค๋ ์๋ฒ์์ http ํต์ ์์๋ง ์ฝ์ ์ ์์์ ํ์, ํ๋ก ํธ์์์ฒ๋ผ JS๋ก ํด๋น ์ฟ ํค๋ฅผ ์ฝ์ผ๋ ค ํ๋ฉด ์น๋ธ๋ผ์ฐ์ ๊ฐ ์ด๋ฅผ ์ฐจ๋จ
๐ ์ค์ ๋ก ์ฟ ํค๊ฐ ์ ๋์ํ๋์ง ํ์ธํ๊ธฐ
index.ejs์์ cookie๊ฐ ์๋ฒ์์ ์ ์ ์ก๋์๋์ง ํ์ธ
(ํ๋ก ํธ์์ ์ฟ ํค ๋ฐํํ ์ฝ๋ ์ฃผ์ ์ฒ๋ฆฌ + ๊ธฐ์กด์ ๋ฐํ๋ ์ฟ ํค๋ ์ญ์ ์ฒ๋ฆฌ)
window.onload = () => {
if (document.cookie) alert(`์ฟ ํค ํ์์! ${document.cookie}`);
};
-> httpOnly ์ต์ ๋๋ฌธ์ document.cookie์ ๊ฐ์ ์ฝ์ผ๋ ค๊ณ ํ๋ ๊ฒ์ ๋ธ๋ผ์ฐ์ ๊ฐ ์ฐจ๋จํ๊ธฐ ๋๋ฌธ์ alert์ฐฝ ์ ๋ธ
๋ฐ๋ผ์, httpOnly ์ต์ ์ false๋ก ๋ฐ๊พธ๊ณ ์คํํ๋ฉด ์๋ํ๋ ๊ฒ์ ํ์ธํ ์ ์์
๐ ์ฟ ํค ๊ฐ ์ ๋ฌ
์ฟ ํค๋ฅผ ์์ฑํ๋ฉด์ ์ฟ ํค์ ๊ฐ์ res.render์ ๋ด์ ๊ฐ์ด ์ ๋ฌํด์ ์ฌ์ฉ ๊ฐ๋ฅ
์์ฑ๋ ์ฟ ํค์ ๋ํ ์ ๊ทผ: req.cookies.์ฟ ํค์ด๋ฆ
res.cookie('alert', true, {
expires: new Date(Date.now() + 1000 * 60),
httpOnly: true,
});
res.render('index', { alert: req.cookies.alert });
// index.ejs
<script>
window.onload = () => {
if ('<%= alert %>' === 'true') alert(`์ฟ ํค ํ์์! ${document.cookie}`);
};
</script>
-> httpOnly ์ต์ ์ด ์ผ์ ธ ์์์๋ ๊ฐ์ ์ ๋ฌํ์๊ธฐ ๋๋ฌธ์ alert์ฐฝ์ด ์ ๋จ๋ ๊ฒ์ ํ์ธํ ์ ์์
(๋จ, ์ฟ ํค ๋ด์ฉ์ ์ ๋ธ)
๐ ํ์ ์ฐฝ ํ๋ฃจ ๋์ ๋ณด์ง ์๊ธฐ
: ํ์ ์ด ๋ฌ ์ํฉ์์ ์ค๋ ํ๋ฃจ ์๋ณด๊ธฐ๋ฅผ ์ฒดํฌํ๊ณ ๋ซ๊ธฐ๋ฅผ ๋๋ฅด๋ฉด ์ฟ ํค๋ฅผ ๋ฐํํ๊ณ , ํด๋น ์ฟ ํค์ ์ํด ํ์ ์ด ํ๋ฃจ ๋์ ์๋ณด์ด๋๋ก ๊ตฌํ
(๋ฐ๋์, app.js์์ index.ejs ๋ผ์ฐํ ๋ณด๋ค cookie-parser ๋ถ๋ฌ์ค๋ ๊ฒ์ ๋จผ์ ํด์ผ ์๋ํ๋ค)
(๋๋ ์๊ณ ์ถ์ง ์์์)
๐index.ejs
<body>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">cookie ์ค์ต</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
์ฟ ํค ์ค์ต
<div class="text-end mt-3">
<input type="checkbox" id="cookie"> <label for="cookie" style="cursor: pointer;">์ค๋ ํ๋ฃจ ์๋ณด๊ธฐ</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="modalClose();">๋ซ๊ธฐ</button>
</div>
</div>
</div>
</div>
<script>
const myModal = new bootstrap.Modal('#exampleModal');
if ('<%= popup %>' !== 'hide') myModal.show();
function modalClose() {
// ํ๋ฃจ ๋์ ์๋ณด๊ธฐ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
const cookie = document.getElementById("cookie");
if (cookie.checked == true) {
fetch('/cookie', {
method: 'post',
headers: {
'Content-type': 'application/json'
},
}).then((res) => {
console.log(res.data);
}).catch((err) => {
console.log(err);
})
}
myModal.hide();
}
</script>
<div class="container mt-5">
<h1 class="text-center">Hello, Express Service</h1>
<h2 class="mt-5 text-center"><a href="/login">๋ก๊ทธ์ธ ๋ฐ๋ก๊ฐ๊ธฐ</a></h2>
</div>
</body>
๐index.js
// @ts-check
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.render('index', { popup: req.cookies.popup });
});
router.post('/cookie', (req, res) => {
res.cookie('popup', 'hide', {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24),
httpOnly: true,
});
res.send('์ฟ ํค ์์ฑ');
});
module.exports = router;
'Server > Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[node.js] passport(local ๋ก๊ทธ์ธ, ๋ก๊ทธ์์ ๊ตฌํ) (0) | 2022.09.24 |
---|---|
[node.js] Session์ผ๋ก ๋ก๊ทธ์ธ ๊ตฌํํ๊ธฐ (0) | 2022.09.24 |
[node.js] Front์์ Back์ผ๋ก ๋ฐ์ดํฐ ์ ์ก (0) | 2022.09.24 |
[node.js] Error Handling (1) | 2022.09.24 |
[node.js] View Engine(ejs, pug, nunjucks) (0) | 2022.09.24 |