koa-cookie/README.md

96 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2017-08-18 17:58:45 +02:00
# koa-cookie
2017-08-24 18:35:39 +02:00
[![npm](https://img.shields.io/npm/v/cca-koa-cookie.svg)](https://www.npmjs.com/package/cca-koa-cookie)
[![npm](https://img.shields.io/npm/dt/cca-koa-cookie.svg)]()
[![npm](https://img.shields.io/npm/l/cca-koa-cookie.svg)]()
2017-08-25 19:49:10 +02:00
[![Travis](https://img.shields.io/travis/CupCakeArmy/koa-cookie.svg)]()
2017-08-24 18:35:39 +02:00
2017-08-18 19:53:20 +02:00
Lightweight Koa-Middleware for Cookies
2017-08-18 18:20:10 +02:00
2017-10-27 15:00:00 +02:00
### Install
```bash
npm install cca-koa-cookie --save
```
2017-10-27 14:58:54 +02:00
### QA
If you have questions:
2017-10-27 14:59:05 +02:00
2017-10-27 14:58:54 +02:00
[![Join the chat at https://gitter.im/cupcakearmy/koa-cookie](https://badges.gitter.im/cupcakearmy/koa-cookie.svg)](https://gitter.im/cupcakearmy/koa-cookie?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2017-08-18 18:20:10 +02:00
### Complete Example
```javascript
const
Koa = require('koa'),
2017-08-21 00:23:50 +02:00
cookie = require('cca-koa-cookie'),
router = require('cca-koa-router')
2017-08-18 18:20:10 +02:00
2017-08-18 18:46:22 +02:00
const
2017-08-18 18:20:10 +02:00
app = new Koa(),
port = 3000
2017-08-18 18:46:22 +02:00
app.use(cookie)
2017-08-18 18:20:10 +02:00
app.use(router({}, _ => {
_.get('/set', (c, n) => {
2017-08-18 18:46:22 +02:00
// Set a cookie
2017-08-18 18:20:10 +02:00
c.request.cookie.set({
key: 'my_id',
value: '12345678',
path: '/',
maxAge: 100,
httpOnly: true,
secure: false
})
2017-08-18 18:46:22 +02:00
2017-08-21 00:23:50 +02:00
c.body = 'Cookie Set'
2017-08-18 18:20:10 +02:00
})
_.get('/get', (c, n) => {
2017-08-21 00:23:50 +02:00
c.body = c.request.cookies
2017-08-18 18:20:10 +02:00
})
}))
app.listen(port)
```
## Documentation
##### ~ `cookies` Array
The Cookie Parser sets an array `ctx.request.cookies` with the cookies that where sent with the request.
###### Example
Let's say we get a cookie (`Foo=Bar;`) We can access it in the context of Koa like this: `ctx.request.cookies['Foo']` => `"bar"`
##### ~ `cookie` Element
The Parser also defines a getter and setter for cookies
###### Example
```javascript
// Set a cookie
2017-08-21 00:23:50 +02:00
ctx.request.cookie.set({
2017-08-18 18:20:10 +02:00
key: 'my_id',
value: '12345678',
path: '/',
maxAge: 100,
httpOnly: true,
secure: false
})
```
2017-08-18 19:50:42 +02:00
|Options | Value |
|--------| -------------------------------|
|Key | Cookie Name |
|Value | Cookie Value |
|Path | Url Path |
|Domain | Sub or Domain |
|httpOnly| bool |
|secure | bool |
|max-age | seconds after the cookie expres|
2017-08-18 18:20:10 +02:00
```javascript
// Get a cookie
ctx.request.cookie.get('my_id') => "12345678"
```