Koa Cookie Utility
Go to file
nicco d1ae9205e8 License 2017-08-18 19:16:21 +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
README.md Update Readme 2017-08-18 18:46:22 +02:00
package.json Version Bump 2017-08-18 18:47:00 +02:00

README.md

koa-cookie

Koa Cookie Utility

Complete Example

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

const
	app = new Koa(),
	port = 3000

// Parse Parameters 
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 = [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"

The Parser also defines a getter and setter for cookies

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