From b047405cc54dc9801c308fddbd1dfea00dec8c43 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Tue, 27 Aug 2013 19:30:35 -0700 Subject: [PATCH] add Context#hasContent --- docs/api.md | 4 ++++ lib/context.js | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index 8656309..1b982c1 100644 --- a/docs/api.md +++ b/docs/api.md @@ -192,6 +192,10 @@ app.context({ if you have a typo an error will be thrown, displaying this list so you can make a correction. +### ctx.hasContent + + When the response status is __204__ or __304__ this returns __false__. + ### ctx.length Return request Content-Length as a number when present, or undefined. diff --git a/lib/context.js b/lib/context.js index 15f7719..a2d3c61 100644 --- a/lib/context.js +++ b/lib/context.js @@ -71,13 +71,26 @@ module.exports = { val = n; } - if (204 == val || 304 == val) { + this.res.statusCode = val; + + if (!this.hasContent) { this.res.removeHeader('Content-Type'); this.res.removeHeader('Content-Length'); this.res.removeHeader('Transfer-Encoding'); } + }, - this.res.statusCode = val; + /** + * Check if the response has content, + * aka is not a 204 or 304 response. + * + * @return {Boolean} + * @api public + */ + + get hasContent() { + var s = this.status; + return 204 != s && 304 != s; }, /**