Skip to content
Myr

Added res.cookie() and res.clearCookie()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 6eeb425 · Browse files · 3 files+54 added−5 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+54 added−5 removed

  • tests:+18 added−0 removed
  • other:+36 added−5 removed

3 files+54 added−5 removed

modified:examples/session/app.js+1 added-1 removed
modified:lib/express/response.js+35 added-4 removed
218 unmodified lines
219 }220};221222/**223 * Clear cookie `name`.224 *225 * @param {String} name226 * @api public227 */228229http.ServerResponse.prototype.clearCookie = function(name){230 this.cookie(name, '', { expires: new Date(1) });231};232233/**234 * Set cookie `name` to `val`.235 *236 * Examples:237 *238 * // "Remember Me" for 15 minutes239 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });240 *241 * @param {String} name242 * @param {String} val243 * @param {Options} options244 * @api public245 */246247http.ServerResponse.prototype.cookie = function(name, val, options){248 var cookie = utils.serializeCookie(name, val, options);249 this.header('Set-Cookie', cookie);250};251252/**253 * Redirect to the given `url` with optional response `status`254 * defauling to 302.255 *256 * The given `url` can also be the name of a mapped url, for
51 unmodified lines
308 : map[url];309310 // Perform redirect311 url = mapped || url;282 this.send('Redirecting to ' + url, {283 'Content-Type': 'text/plain',284 'Location': url285 }, status || 302);312313 // Respond314 this.headers['Content-Type'] = 'text/plain';315 this.headers['Location'] = url;316 this.send('Redirecting to ' + url, this.headers, status || 302);317};
modified:test/response.test.js+18 added
235 unmodified lines
236 { body: '{"name":"tj"}', status: 200, headers: {237 'Content-Type': 'application/json',238 'Content-Disposition': 'attachment; filename="account.json"'239 }});240 },241 242 'test #cookie()': function(assert){243 var app = express.createServer();244 245 app.get('/', function(req, res){246 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });247 res.redirect('/');248 });249 250 assert.response(app,251 { url: '/' },252 function(res){253 assert.length(res.headers['set-cookie'], 1);254 assert.length(255 'rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly',256 res.headers['set-cookie'][0]);257 });258 }259};