Code Monkey home page Code Monkey logo

koa-cookie's Introduction

koa-cookie

npm npm npm Travis

Lightweight Koa-Middleware for Cookies

Install

npm install cca-koa-cookie --save

QA

If you have questions:

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

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"

~ cookie Element

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"

koa-cookie's People

Contributors

cupcakearmy avatar gitter-badger avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

gitter-badger

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.