Helper comments
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 9ae82c2 · Browse files · 1 file·+77 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+77 added−1 removed
- other:+77 added−1 removed
Provenance
- Commit
99dc356- Tree
1d69f6d- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
9ae82c2
1 file changed+77 added−1 removed
modified:lib/express/helpers.js+77 added-1 removed
12344567891033 unmodified lines444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515651 unmodified lines
// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// --- Default response code bodies/** * Default response code bodies. */
var statusBodies = { 100: 'Continue', 101: 'Switching Protocols',33 unmodified lines 504: 'Gateway Time-out', 505: 'HTTP Version not supported'}
/** * JSON encode _object_. * * @param {string} object * @return {string} * @api public */
exports.jsonEncode = function(object) { return JSON.stringify(object)}
/** * JSON decode _string_. * * @param {string} string * @return {mixed} * @api public */
exports.jsonDecode = function(string) { return JSON.parse(string)}
/** * Return the directory name of the given _path_. * * @param {string} path * @return {string} * @api public */
exports.dirname = function(path) { return path.split('/').slice(0, -1).join('/')}
/** * Return the extension name of the given _path_, * or null when not present. * * @param {string} path * @return {string} * @api public */
exports.extname = function(path) { if (path.lastIndexOf('.') < 0) return return path.slice(path.lastIndexOf('.') + 1)}
/** * Return a placeholder route param keyed * with the given _key_ or null. * * @param {string} key * @return {string} * @api public */
exports.param = function(key) { return Express.router.params[key]}
/** * Get or set the header field _key_ with _val_. * * @param {string} key * @param {string} val * @return {string} * @api public */
exports.header = function(key, val) { return value === undefined ? Express.server.response.headers[key.toLowerCase()] : Express.server.response.headers[key.toLowerCase()] = val}
/** * Set response status to _code_. * * @param {int} code * @return {int} * @api public */
var status = exports.status = function(code) { return Express.server.response.status = code}
/** * 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)51 unmodified lines