Added Express.cookie() for get/setting
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 20cdbde · Browse files · 3 files·+56 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+56 added−6 removed
- tests:+19 added−0 removed
- other:+37 added−6 removed
Provenance
- Commit
9f4d2be- Tree
0debe68- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
20cdbde
3 files changed+56 added−6 removed
modified:lib/express.core.js+35 added-6 removed
2 unmodified lines3 Express = { 4 version : '0.0.1',5 routes : [],6 7+ // --- Response8+ 9 response : {10 body : null,11 status : 200,10- headers : {}12+ headers : {},13+ cookie : {}14 },15 16+ // --- Defaults17+ 18+ defaults : {19+ cookie : {20+ expires : 30021+ },13- defaultRoute : {14- callback : function() {15- Express.status('Not Found')16- 'Not Found'22+ route : {23+ callback : function() {24+ Express.status('Not Found')25+ 'Not Found'26+ }27 }28 },29 30 /**112 unmodified lines143 param : function(key, defaultValue) {144 return this.request.uri.params[key] || defaultValue145 },146 147+ 148 /**149+ * Return cookie _key_. When _value_ is present150+ * then a response cookie field will be set.151+ *152+ * @param {string} key153+ * @param {string} value154+ * @return {string, hash}155+ * @api public156+ */157+158+ cookie : function(key, value) {159+ if (!value) return this.request.cookie[key]160+ return this.response.cookie[key] = {161+ key : key,162+ value : value163+ }164+ },165+ 166+ /**167 * Parse _params_, splitting { user[info][name] : 'tj' } into168 * { user : { info : { name : 'tj' }}}169 *170 * @param {hash} params192 unmodified lines363 * @api private364 */365 366 callRouteFor : function(request) {338- var route = this.findRouteFor(request) || this.defaultRoute367+ var route = this.findRouteFor(request) || this.defaults.route368 if (route.keys)369 for (i = 0, len = route.keys.length; i < len; ++i) 370 this.request.uri.params[route.keys[i]] = Express.captures[i+1] || null371 return this.eval(Express.contentsOf(route.callback)).toString()109 unmodified linesmodified:spec/spec.core.js+19 added
67 unmodified lines68697071727374757677787980818283848586878889909192939470 unmodified lines67 unmodified lines parts.domain.should.eql 'example.net' end end describe '.cookie()' before_each cookie = 'expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; q=foo%3dbar; domain=example.net' Express.request = {} Express.request.cookie = Express.parseCookie(cookie) end it 'should return cookie value when key passed' Express.cookie('path').should.eql '/' Express.cookie('domain').should.eql 'example.net' end it 'should set response cookies when value passed' Express.cookie('foo', 'bar') Express.response.cookie.foo.key.should.eql 'foo' Express.response.cookie.foo.value.should.eql 'bar' end end describe '.argsArray()' it 'should return an array of arguments' Express.argsArray(-{ return arguments }('foo', 'bar')).should.eql ['foo', 'bar'] end70 unmodified linesmodified:README.rdoc+2 added
10 unmodified lines11 * Sexy DSL12 * Mime helpers13 * Nested parameter parsing (user[name]=tj == { user : { name : 'tj' }})14 * Full test coverage15+* Cookie support16+* Non polluting (so you can use it with your fav JavaScript libraries :) )17 * Routing18 * string matching19 * regexp with captures20 * param key substitution60 unmodified lines