koa-cookie/README.md

80 lines
1.6 KiB
Markdown
Raw Normal View History

2017-08-18 15:58:45 +00:00
# koa-cookie
2017-08-18 17:53:20 +00:00
Lightweight Koa-Middleware for Cookies
2017-08-18 16:20:10 +00:00
### Complete Example
```javascript
const
Koa = require('koa'),
2017-08-18 16:46:22 +00:00
router = require('koa-simple-router'),
2017-08-18 16:20:10 +00:00
cookie = require('cca-koa-cookie')
2017-08-18 16:46:22 +00:00
const
2017-08-18 16:20:10 +00:00
app = new Koa(),
port = 3000
2017-08-18 16:46:22 +00:00
// Parse Parameters
app.use(cookie)
2017-08-18 16:20:10 +00:00
app.use(router({}, _ => {
_.get('/set', (c, n) => {
2017-08-18 16:46:22 +00:00
// Set a cookie
2017-08-18 16:20:10 +00:00
c.request.cookie.set({
key: 'my_id',
value: '12345678',
path: '/',
maxAge: 100,
httpOnly: true,
secure: false
})
2017-08-18 16:46:22 +00:00
2017-08-18 16:20:10 +00:00
c.body = [200, 'Cookie Set']
})
_.get('/get', (c, n) => {
c.body = [200, c.request.cookies]
})
}))
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
c.request.cookie.set({
key: 'my_id',
value: '12345678',
path: '/',
maxAge: 100,
httpOnly: true,
secure: false
})
```
2017-08-18 17:50:42 +00: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 16:20:10 +00:00
```javascript
// Get a cookie
ctx.request.cookie.get('my_id') => "12345678"
```