Skip to content

Commit

Permalink
feat: Support custom Express server
Browse files Browse the repository at this point in the history
Feature support custom express server
  • Loading branch information
maticzav authored Aug 3, 2020
2 parents 4ad83e9 + 34b975a commit e2b43fd
Show file tree
Hide file tree
Showing 4 changed files with 14,428 additions and 5 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
A collection of cookie helpers for Next.js

- SSR support, for setter, parser and destroy
- Custom Express server support
- super light
- perfect for authentication

Expand Down Expand Up @@ -116,19 +117,51 @@ export default function Me() {
}
```

## Custom Express server cookies

```js
const express = require('express');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const { parseCookies, setCookie, destroyCookie } = require('nookies');

app.prepare()
.then(() => {
const server = express();

server.get('/page', (req, res) => {

// Notice how the request object is passed
const parsedCookies = parseCookies({ req });

// Notice how the response object is passed
setCookie({ res }, 'fromServer', 'value', {
maxAge: 30 * 24 * 60 * 60,
path: '/page',
});

// destroyCookie({ res }, 'fromServer');

return handle(req, res);
});

);
```
## Reference
> For client side usage, omit the `ctx` parameter. You can do so by setting it to an empty object (`{}`).
### `parseCookies(ctx, options)` or `cookies.get(ctx, options)`
- **ctx:** `Next.js context`
- **ctx:** `Next.js context` || `(Express request object)`
- **options:**
- **decode:** `a custom resolver function (default: decodeURIComponent)`

### `setCookie(ctx, name, value, options)` or `cookies.set(ctx, name, value, options)`

- **ctx:** `(Next.js context)`
- **ctx:** `(Next.js context)` || `(Express request object)`
- **name:** cookie name
- **value:** cookie value
- **options:**
Expand All @@ -143,7 +176,7 @@ export default function Me() {

### `destroyCookie(ctx, name, options)` or `cookies.destroy(ctx, 'token', options)`

- **ctx:** (Next.js context)
- **ctx:** `(Next.js context)` || `(Express response object)`
- **name:** cookie name
- **options:**
- **domain**
Expand Down
Loading

0 comments on commit e2b43fd

Please sign in to comment.