# @forrestjs/service-express-cookies

> ForrestJS service which helps handling cookies in an ExpressJS server.

Latest version **3.19.0** (published 2020-10-03) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @forrestjs/service-express-cookies
pnpm add @forrestjs/service-express-cookies
yarn add @forrestjs/service-express-cookies
bun add @forrestjs/service-express-cookies
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.19.0 |
| Published | 2020-10-03 |
| First published | 2019-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Marco Pegoraro |
| Maintainers | marcopeg |
| Keywords | forrestjs, service, express, cookie |

## Links

- npm: https://www.npmjs.com/package/@forrestjs/service-express-cookies
- Repository: https://github.com/forrestjs/forrestjs/tree/master/packages/service-express-cookies
- Homepage: https://github.com/forrestjs/forrestjs/tree/master/packages/service-express-cookies#readme
- Issues: https://github.com/forrestjs/forrestjs/issues
- npm.io page: https://npm.io/package/@forrestjs/service-express-cookies

## Dependencies (5)

- [millisecond](https://npm.io/package/millisecond.md) ^0.1.2
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.5
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.11.2
- [@forrestjs/hooks](https://npm.io/package/@forrestjs/hooks.md) ^3.19.0
- [@forrestjs/service-express](https://npm.io/package/@forrestjs/service-express.md) ^3.19.0

## Recent versions

- 3.19.0 (latest) — 2020-10-03
- 3.18.2 — 2020-09-30
- 3.18.1 — 2020-09-28
- 3.18.0 — 2020-09-28
- 3.17.0 — 2020-09-25
- 3.16.1 — 2020-09-13
- 3.16.0 — 2020-08-14
- 3.15.0 — 2020-08-13
- 3.14.0 — 2020-08-09
- 3.13.8 — 2020-08-09
- 3.13.7 — 2020-08-06
- 3.13.6 — 2020-08-06
- 3.13.5 — 2020-08-06
- 3.13.4 — 2020-08-06
- 3.13.3 — 2020-08-05
- … 100 more at https://npm.io/package/@forrestjs/service-express-cookies/versions

## README

# @forrestjs/service-express-cookies

ForrestJS service which helps handling cookies in an ExpressJS server.

## Usage

This extension decorates the `req` and `res` object in an `ExpressJS` request.
Use the methods as in the following example to read/write to cookies.

```js
// Server Side Cookies
res.setCookie('cookieName', 'value', { /* override options */ })
res.deleteCookie('cookieName')
req.getCookie('cookieName') // -> value

// Client Side Cookies
res.setClientCookie('cookieName', 'value', { /* override options */ })
res.deleteClientCookie('cookieName')
req.getClientCookie('cookieName') // -> value
```

**AppCookies** can not be accessed by the client and go in `https-only` by default
when `NODE_ENV=production`.

**ClientCookies** can be accessed by the client and therefore are not super safe.
_Use those with caution_.

The `override options` argument is optional and allow to override any pre-configured
setting that is passed down to `res.cookie`. 

**NOTE:** `maxAge` can be set as string like `5 minutes` and gets parsed by
[millisecond](https://www.npmjs.com/package/millisecond).

## Configuration

```js
const { runHookApp } = require('@forrestjs/hooks')

runHookApp({
    settings: {
        express: {
            port: 8080,
        },
        expressCookies: {
            scope: 'XXX',
            secure: false,
            httpOnly: true,
            duration: '30d',
            separator: '::',
            clientDuration: '30d',
            clientSeparator: '--',
        }
    },
    services: [
        require('@forrestjs/service-express'),
        require('@forrestjs/service-express-cookies'),
    ],
    features: [
        ({ getHook }) => [getHook('EXPRESS_ROUTE'), ({ registerRoute }) => {
            registerRoute.get('/', (req, res) => {
                res.setCookie('foo', 123)
                res.setClientCookie('foo', 123)
                res.send('ok')
            })
        }]
    ]
})
```

## scope

`scope` is a string that prefix any cookie name you will ever use. It's cool to work
on multiple apps in development and still avoid conflicts ;-)

## secure

Forces cookies to apply only to `https` connections.

default: `isDevOrTest ? false : true`

> it applies to Server Side cookies only

## httpOnly

Hides cookies from client side access.

default: `true`

> it applies to Server Side cookies only

## duration

default: `300y`

## separator

default: `::`

## clientDuration

default: ${duration}

## clientSeparator

default: `--`

---
_Source: https://npm.io/package/@forrestjs/service-express-cookies · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
