1const cookieParser = require('cookie-parser');
2const express = require('express');
3const app = express();
4app.use(cookieParser());
5
6app.get('/', (req, res) => {
7 console.log('Cookies: ', req.cookies);
8 console.log('Signed Cookies: ', req.signedCookies);
9});
10
11app.listen(3000);
1function deleteAllCookies() {
2 var cookies = document.cookie.split(";");
3
4 for (var i = 0; i < cookies.length; i++) {
5 var cookie = cookies[i];
6 var eqPos = cookie.indexOf("=");
7 var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
8 document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
9 }
10}
11
12deleteAllCookies();
1function deleteCookie(name) {
2 document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
3}
1function setCookie(name,value,days) {
2 var expires = "";
3 if (days) {
4 var date = new Date();
5 date.setTime(date.getTime() + (days*24*60*60*1000));
6 expires = "; expires=" + date.toUTCString();
7 }
8 document.cookie = name + "=" + (value || "") + expires + "; path=/";
9}
10function getCookie(name) {
11 var nameEQ = name + "=";
12 var ca = document.cookie.split(';');
13 for(var i=0;i < ca.length;i++) {
14 var c = ca[i];
15 while (c.charAt(0)==' ') c = c.substring(1,c.length);
16 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
17 }
18 return null;
19}
20
21setCookie("user_email","bobthegreat@gmail.com",30); //set "user_email" cookie, expires in 30 days
22var userEmail=getCookie("user_email");//"bobthegreat@gmail.com"
1function setCookie(cname, cvalue, exdays=999) {
2 var d = new Date();
3 d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
4 var expires = "expires="+d.toUTCString();
5 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
6}
7
8function getCookie(cname) {
9 var name = cname + "=";
10 var ca = document.cookie.split(';');
11 for(var i = 0; i < ca.length; i++) {
12 var c = ca[i];
13 while (c.charAt(0) == ' ') {
14 c = c.substring(1);
15 }
16 if (c.indexOf(name) == 0) {
17 return c.substring(name.length, c.length);
18 }
19 }
20 return "";
21}
1const setCookie = (options) => {
2 const {
3 name,
4 value = '',
5 path = '/',
6 duration = 3600,
7 } = options;
8
9 const durationMs = duration * 1000;
10 const expires =
11 new Date(Date.now() + durationMs);
12
13 document.cookie =
14 `${name}=${escape(value)}; expires=${expires.toUTCString()}; path=${path}`;
15}
16
17const getCookie = (name, cast = String) => {
18 if (document.cookie.length == 0)
19 return;
20
21 const match = document
22 .cookie
23 .match(`${name}=(?<value>[\\w]*);?`);
24
25 if (!match)
26 return;
27
28 const value =
29 match?.groups?.value ?? '';
30
31 return cast(unescape(value));
32}
33
34const cookieExists = (name) => {
35 return getCookie(name) !== undefined;
36}
37
38const deleteCookie = (name) => {
39 setCookie({
40 name: name,
41 value: undefined,
42 duration: -1,
43 });
44}
45
46
47// Example string
48setCookie({
49 name: 'username',
50 value: 'dude',
51});
52
53const username =
54 getCookie('username');
55
56
57// Example number
58setCookie({
59 name: 'count',
60 value: 100,
61 duration: 300, // 300s, 5 minutes
62});
63
64const count =
65 getCookie('count', parseInt);
66
67deleteCookie('count');