Koa Cookie Utility
Go to file
The Gitter Badger a22d4c1be1 Add Gitter badge
2017-10-27 12:54:57 +00:00
.gitignore Added Testing & Travis 2017-08-25 19:44:17 +02:00
.travis.yml Added Testing & Travis 2017-08-25 19:44:17 +02:00
Cookie.js Added Package.json 2017-08-18 18:03:30 +02:00
LICENSE License 2017-08-18 19:16:21 +02:00
package.json Added Testing & Travis 2017-08-25 19:44:17 +02:00
README.md Add Gitter badge 2017-10-27 12:54:57 +00:00
test.js Added Testing & Travis 2017-08-25 19:44:17 +02:00

koa-cookie

Join the chat at https://gitter.im/cupcakearmy/koa-cookie

npm npm npm Travis

Lightweight Koa-Middleware for Cookies

Install

npm install cca-koa-cookie --save

Complete Example

const
	Koa = require('koa'),
	cookie = require('cca-koa-cookie'),
	router = require('cca-koa-router')

const
	app = new Koa(),
	port = 3000

app.use(cookie)

app.use(router({}, _ => {
	_.get('/set', (c, n) => {

		// Set a cookie 
		c.request.cookie.set({
			key: 'my_id',
			value: '12345678',
			path: '/',
			maxAge: 100,
			httpOnly: true,
			secure: false
		})

		c.body = 'Cookie Set'
	})

	_.get('/get', (c, n) => {
		c.body = 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"

The Parser also defines a getter and setter for cookies

Example
// Set a cookie
ctx.request.cookie.set({
	key: 'my_id',
	value: '12345678',
	path: '/',
	maxAge: 100,
	httpOnly: true,
	secure: false
})
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
// Get a cookie
ctx.request.cookie.get('my_id') => "12345678"