# cookie parser

***

**1. Parsing Cookies in Express.js**

```js
const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/', (req, res) => {
  res.send(req.cookies); // Outputs the parsed cookies object
});
```

**2. Reading a Specific Cookie**

```js
const cookie = req.cookies['my_cookie']; // Returns the value of the cookie named 'my_cookie'
```

**3. Parsing Signed Cookies**

```js
app.use(cookieParser('my_secret')); // Signs cookies with 'my_secret'

req.cookies.my_signed_cookie; // Returns the value of the signed cookie, verifying its integrity
```

**4. Parsing JSON Cookies**

```js
const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser('my_secret', { json: true }));

app.get('/', (req, res) => {
  res.json({ user: 'alice' }); // Sets a JSON cookie named 'user'
});

req.cookies.user; // Returns the parsed JSON object from the cookie
```

**5. Deleting a Cookie**

```js
res.clearCookie('my_cookie'); // Deletes the cookie named 'my_cookie'
```

**6. Setting a Cookie with a Custom Expiration Date**

```js
res.cookie('my_cookie', 'value', { expires: new Date('2024-01-01') }); // Sets a cookie that expires on January 1, 2024
```

**7. Using a Domain to Limit Cookie Access**

```js
res.cookie('my_cookie', 'value', { domain: '.example.com' }); // Restricts the cookie to the specified domain
```

**8. Setting a Secure Cookie**

```js
res.cookie('my_cookie', 'value', { secure: true }); // Requires the cookie to be transmitted over HTTPS
```

**9. Setting a HttpOnly Cookie**

```js
res.cookie('my_cookie', 'value', { httpOnly: true }); // Prevents client-side JavaScript from accessing the cookie
```

**10. Using a Cookie-Parser Middleware**

```js
const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/', (req, res) => {
  // Access parsed cookies in the request object
});
```

**11. Parsing Encrypted Cookies**

```js
const cookieParser = require('cookie-parser');
const crypto = require('crypto');

const secretKey = 'my_secret_key';
const encryptedCookies = crypto.encrypt('aes-256-cbc', secretKey, Buffer.from('my_cookie_data'));

const parsedCookies = cookieParser.signedCookies(encryptedCookies, secretKey); // Decrypts and parses the cookies
```

**12. Configuring Multiple Cookie Parsers**

```js
const app = express();
app.use('/api', cookieParser()); // Use the default cookie parser for '/api' routes

app.use('/admin', cookieParser('my_secret', { json: true })); // Use a custom cookie parser for '/admin' routes
```

**13. Parsing Cookies in Fastify**

```js
const fastify = require('fastify');
const cookieParser = require('fastify-cookie');

fastify.register(cookieParser);

fastify.get('/', (request, reply) => {
  reply.send(request.cookies); // Outputs the parsed cookies object
});
```

**14. Setting a Cookie with Fastify**

```js
fastify.get('/set-cookie', (request, reply) => {
  reply.setCookie('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
});
```

**15. Parsing Cookies in Koa**

```js
const Koa = require('koa');
const cookieParser = require('koa-cookie');

const app = new Koa();
app.use(cookieParser());

app.use(ctx => {
  ctx.body = ctx.cookies; // Outputs the parsed cookies object
});
```

**16. Setting a Cookie with Koa**

```js
const app = new Koa();

app.use(ctx => {
  ctx.cookies.set('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
});
```

**17. Parsing Cookies in Hapi.js**

```js
const Hapi = require('hapi');
const cookieParser = require('hapi-auth-cookie');

const server = new Hapi.Server();
server.register({
  plugin: cookieParser,
  options: {
    cookie: 'my_cookie',
    password: 'my_secret',
  },
});
```

**18. Setting a Cookie with Hapi.js**

```js
server.route({
  method: 'GET',
  path: '/set-cookie',
  handler: (request, h) => {
    return h.response('My cookie is set!').state('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
  },
});
```

**19. Parsing Cookies in Restify**

```js
const restify = require('restify');
const cookieParser = require('restify-cookies');

const server = restify.createServer();
server.use(cookieParser());

server.get('/', (req, res, next) => {
  res.send(req.cookies); // Outputs the parsed cookies object
  next();
});
```

**20. Setting a Cookie with Restify**

```js
server.get('/set-cookie', (req, res, next) => {
  res.setCookie('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
  res.send('My cookie is set!');
  next();
});
```

**21. Parsing Cookies in Socket.IO**

```js
const io = require('socket.io')(3000);

io.use((socket, next) => {
  cookieParser('my_secret')(socket.request, socket.request.res, next);
});

io.on('connection', (socket) => {
  console.log(socket.request.cookies); // Outputs the parsed cookies object
});
```

**22. Setting a Cookie with Express and Socket.IO**

```js
const express = require('express');
const cookieParser = require('cookie-parser');
const socketIO = require('socket.io');

const app = express();
const server = app.listen(3000);
const io = socketIO(server);

app.use(cookieParser());

app.get('/', (req, res) => {
  res.cookie('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
});

io.on('connection', (socket) => {
  console.log(socket.request.cookies); // Outputs the parsed cookies object
});
```

**23. Parsing Cookies in Nest.js**

```js
import { Injectable, MiddlewareConsumer, NestMiddleware } from '@nestjs/common';
import * as cookieParser from 'cookie-parser';

@Injectable()
export class CookieParsingMiddleware implements NestMiddleware {
  use(req, res, next) {
    cookieParser()(req, res, next);
  }
}

export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(CookieParsingMiddleware).forRoutes('*');
  }
}
```

**24. Setting a Cookie with Nest.js**

```js
@Controller()
export class AppController {
  @Get('/set-cookie')
  setCookie(@Res() res: Response) {
    res.cookie('my_cookie', 'value', { path: '/' }); // Sets a cookie with a path of '/'
  }
}
```

**25. Parsing Cookies in Next.js**

```js
import { useEffect } from 'react';
import cookieParser from 'cookie-parser';

const App = ({ cookies }) => {
  useEffect(() => {
    console.log(cookies); // Outputs the parsed cookies object
  }, []);

  return (
    <div>My cookies: {JSON.stringify(cookies)}</div>
  );
};

export async function getServerSideProps({ req, res }) {
  const parsedCookies = cookieParser(req, res);

  return {
    props: {
      cookies: parsedCookies,
    },
  };
}

export default App;
```
