diff --git a/examples/cookies.js b/examples/cookies.js new file mode 100644 index 0000000..fed653e --- /dev/null +++ b/examples/cookies.js @@ -0,0 +1,16 @@ + +var http = require('http'); +var koa = require('..'); +var app = koa(); + +app.use(function(){ + return function *(){ + if ('/favicon.ico' == this.path) return; + var n = ~~this.cookies.get('view') || 1; + this.cookies.set('view', n + 1); + this.body = n + ' views'; + } +}); + +app.listen(3000); +console.log('listening on port 3000'); \ No newline at end of file diff --git a/lib/application.js b/lib/application.js index 8bcb0ab..3a412a5 100644 --- a/lib/application.js +++ b/lib/application.js @@ -7,6 +7,7 @@ var debug = require('debug')('koa:app'); var Emitter = require('events').EventEmitter; var compose = require('koa-compose'); var context = require('./context'); +var Cookies = require('cookies'); var Stream = require('stream'); var http = require('http'); var co = require('co'); @@ -217,5 +218,6 @@ function createContext() { this.app = app; this.req = req; this.res = res; + this.cookies = new Cookies(req, res); } } diff --git a/package.json b/package.json index 0487a61..45fe1e6 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "qs": "0.6.5", "fresh": "0.2.0", "negotiator": "0.2.7", - "koa-compose": "1.0.0" + "koa-compose": "1.0.0", + "cookies": "~0.3.6" }, "devDependencies": { "bytes": "*",