Moving more helpers to Request methods
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent a71a601 · Browse files · 2 files·+88 added−90 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+88 added−90 removed
- other:+88 added−90 removed
Provenance
- Commit
ca3f525- Tree
920c3aa- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
a71a601
2 files changed+88 added−90 removed
modified:lib/express/helpers.js-90 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859567825 unmodified lines34353637939495969798991001011021031041051061071081091101111121131143839404195 unmodified lines137138139140218219220221222223224225226227228229230
// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
/** * Default response code bodies. */
var statusBodies = { 100: 'Continue', 101: 'Switching Protocols', 200: 'OK', 201: 'Created', 202: 'Accepted', 203: 'Non-Authoritative Information', 204: 'No Content', 205: 'Reset Content', 206: 'Partial Content', 300: 'Multiple Choices', 301: 'Moved Permanently', 302: 'Moved Temporarily', 303: 'See Other', 304: 'Not Modified', 305: 'Use Proxy', 400: 'Bad Request', 401: 'Unauthorized', 402: 'Payment Required', 403: 'Forbidden', 404: 'Not Found', 405: 'Method Not Allowed', 406: 'Not Acceptable', 407: 'Proxy Authentication Required', 408: 'Request Time-out', 409: 'Conflict', 410: 'Gone', 411: 'Length Required', 412: 'Precondition Failed', 413: 'Request Entity Too Large', 414: 'Request-URI Too Large', 415: 'Unsupported Media Type', 500: 'Internal Server Error', 501: 'Not Implemented', 502: 'Bad Gateway', 503: 'Service Unavailable', 504: 'Gateway Time-out', 505: 'HTTP Version not supported'}
// --- InvalidStatusCode
InvalidStatusCode = ExpressError.extend({ name: 'InvalidStatusCode', init: function(status) { this.message = status + ' is an invalid HTTP response code' }})
// --- Helpers
/** * JSON aliases. */
JSON.encode = JSON.stringify25 unmodified lines return path.slice(path.lastIndexOf('.') + 1)}
/** * Immediately respond with response _code_ and _body_. * The status _code_ defaults to to 404, and _body_ will * default to the default body associated with the response * _code_. * * When an invalid status _code_ is passed, InvalidStatusCode * will be thrown. * * @param {int} code * @param {string} body * @see statusBodies * @api public */
exports.halt = function(code, body) { status(code = code || 404) if (body = body || statusBodies[code]) return Express.server.respond(body) throw new InvalidStatusCode(code)}
/** * Escape special characters in _html_. * * @param {string} html * @return {string}95 unmodified lines params[pair[0]] = pair[1] return params }))}
/** * Set Content-Type header to the mime type * for the given _path_, which calls mime(). * * @param {string} path * @see mime() * @api public */
exports.contentType = function(path) { header('content-type', mime(path))}No newline at end of filemodified:lib/express/request.js+88 added
1 2 // Express - Request - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4+/**5+ * Default response code bodies.6+ */7+8+var statusBodies = {9+ 100: 'Continue',10+ 101: 'Switching Protocols',11+ 200: 'OK',12+ 201: 'Created',13+ 202: 'Accepted',14+ 203: 'Non-Authoritative Information',15+ 204: 'No Content',16+ 205: 'Reset Content',17+ 206: 'Partial Content',18+ 300: 'Multiple Choices',19+ 301: 'Moved Permanently',20+ 302: 'Moved Temporarily',21+ 303: 'See Other',22+ 304: 'Not Modified',23+ 305: 'Use Proxy',24+ 400: 'Bad Request',25+ 401: 'Unauthorized',26+ 402: 'Payment Required',27+ 403: 'Forbidden',28+ 404: 'Not Found',29+ 405: 'Method Not Allowed',30+ 406: 'Not Acceptable',31+ 407: 'Proxy Authentication Required',32+ 408: 'Request Time-out',33+ 409: 'Conflict',34+ 410: 'Gone',35+ 411: 'Length Required',36+ 412: 'Precondition Failed',37+ 413: 'Request Entity Too Large',38+ 414: 'Request-URI Too Large',39+ 415: 'Unsupported Media Type',40+ 500: 'Internal Server Error',41+ 501: 'Not Implemented',42+ 502: 'Bad Gateway',43+ 503: 'Service Unavailable',44+ 504: 'Gateway Time-out',45+ 505: 'HTTP Version not supported'46+}47+48+// --- InvalidStatusCode49+50+InvalidStatusCode = ExpressError.extend({51+ name: 'InvalidStatusCode',52+ init: function(status) {53+ this.message = status + ' is an invalid HTTP response code'54+ }55+})56+57 // --- Helpers58 59 /**60 * Normalize the given _path_.100 unmodified lines161 return this.response.status = code162 },163 164 /**165+ * Immediately respond with response _code_ and _body_.166+ * The status _code_ defaults to to 404, and _body_ will167+ * default to the default body associated with the response168+ * _code_.169+ *170+ * When an invalid status _code_ is passed, InvalidStatusCode171+ * will be thrown.172+ *173+ * @param {int} code174+ * @param {string} body175+ * @see statusBodies176+ * @api public177+ */178+179+ halt: function(code, body) {180+ this.status(code = code || 404)181+ if (body = body || statusBodies[code])182+ return Express.server.respond(body)183+ throw new InvalidStatusCode(code)184+ },185+ 186+ /**187+ * Set Content-Type header to the mime type188+ * for the given _path_, which calls mime().189+ *190+ * @param {string} path191+ * @see mime()192+ * @api public193+ */194+195+ contentType: function(path) {196+ this.header('content-type', mime(path))197+ },198+ 199+ /**200 * Trigger even _name_ with optional _data_.201 *202 * @param {string} name203 * @param {hash} data13 unmodified lines