Skip to content
Myr

Merge branch 'rename-halt'

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

authored by visionmedia · committed by visionmedia · parents 0352b97, b90a3db · Browse files · 10 files+50 added−72 removed
This commit

State

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

Diff stats

10 files+50 added−72 removed

  • tests:+5 added−31 removed
  • other:+45 added−41 removed

10 files+50 added−72 removed

modified:examples/chat/app.js+3 added-3 removed
modified:lib/express/plugins/redirect.js+1 added-1 removed
modified:lib/express/plugins/static.js+1 added-1 removed
63 unmodified lines
64 : self.error(err, callback)65 var etag = stat.ino + '-' + stat.size + '-' + Number(stat.mtime)66 if (self.header('If-None-Match') && 67 self.header('If-None-Match') == etag)68 return self.halt(304, null)68 return self.respond(304, null)69 self.header('Content-Length', stat.size)70 self.header('ETag', etag)71 self.stream(fs.createReadStream(path, options))72 })
22 unmodified lines
modified:lib/express/plugins/view.js+2 added-2 removed
121 unmodified lines
122 * better error reporting.123 *124 * Options:125 *126 * - encoding: Passed to Request#halt()126 * - encoding: Passed to Request#respond()127 * - layout: The layout to use, none when falsey. Defaults to 'layout'128 * - locals: Most engines support a hash of local variable names / values.129 * - context: Most engines support an evaluation context (the 'this' keyword). 130 * Defaults to the current Request instance.
32 unmodified lines
163 return content164 else if (callback)165 callback.call(this, null, content)166 else167 this.halt(200, content, options.encoding)167 this.respond(200, content, options.encoding)168 }169 })170 }171 }
2 unmodified lines
modified:lib/express/core.js+1 added-1 removed
278 unmodified lines
279 try {280 if (err) throw err281 if (request.response.finished) return282 if (typeof (body = (new Router(request)).route()) === 'string')283 request.halt(200, body, function(err){283 request.respond(200, body, function(err){284 if (err) request.error(err)285 }) 286 } catch (err) {287 request.error(err)
40 unmodified lines
modified:lib/express/request.js+37 added-33 removed
9 unmodified lines
10 notFound = require('express/pages/not-found'),11 statusBodies = require('http').STATUS_CODES,12 queryString = require('querystring'),13 mime = require('express/mime'),14 url = require('url')14 url = require('url'),15 ext = require('ext')1617// --- Helpers1819/**
132 unmodified lines
152 return this153 },154 155 /**155 * Immediately respond with response _code_, _body_.156 * Respond with response _code_, and optional _body_.157 *158 * The status _code_ defaults to to 404, and _body_ will159 * default to the default body associated with the response160 * _code_.161 *161 * Optionally an _encoding_ may be passed, followed by162 * a _callback_ function.162 * Examples:163 *164 * request.respond()165 * // 404 "Not Found"166 * 167 * request.respond(500)168 * // 500 "Internal Server Error"169 * 170 * request.respond(200, "im ok")171 * // 200 "im ok"172 *173 * request.respond(200, "√ woot", "utf8")174 * // "utf8" will set Request#charset to 'UTF-8'175 *176 * @param {int} code177 * @param {string} body178 * @param {string} encoding167 * @param {function} callback179 * @see statusBodies180 * @api public181 */182172 halt: function(code, body, encoding, callback) {173 if (encoding instanceof Function)174 callback = encoding,175 encoding = null183 respond: function(code, body, encoding) {184 var self = this185 this.status(code = code || 404)186 if (body !== null)187 body = body || statusBodies[code]188 if (encoding === 'utf8' ||189 encoding === 'utf-8')190 this.charset = 'UTF-8'182 return this.respond(body, encoding, callback)183 },184 185 /**186 * Respond with _body_ and optional _encoding_.187 *188 * @param {string} body189 * @param {string} encoding190 * @see Request#halt()191 * @api private192 */193 194 respond: function(body, encoding, callback) {195 var self = this191 this.response.body = body192 this.trigger('response', function(err) {198 if (err) self.error(err, callback)193 if (err) return self.error(err)194 self.sendHead()195 if (body) self.response.write(self.response.body, encoding)196 self.response.end()197 }, true)198 },199 200 // >>> DEPRECATED: remove in 1.0201 202 halt: function(){203 ext.warn('Request#halt() has been renamed Request#respond()')204 this.respond.apply(this, arguments)205 },206 207 // <<< DEPRECATED208 209 /**210 * Transfer the given _stream_ with optional _callback_.211 *212 * @param {Stream} stream
137 unmodified lines
350 * will then be called regardless of any setting that351 * may be present. ("throw exceptions" will still work)352 *353 * error(function(err){350 * this.halt(500, 'your app sucks!')354 * this.respond(500, 'your app sucks!')355 * })356 *357 * When "show exceptions" is enabled the show-exceptions page will be shown for "text/html",358 * a plain-text representation of the error for "text/plain" and JSON for "application/json".
18 unmodified lines
377 if (Express.error)378 Express.error.call(this, err)379 else if (set('show exceptions'))380 if (this.accepts('html'))377 this.halt(500, showExceptions.render(this, err))381 this.respond(500, showExceptions.render(this, err))382 else if (this.accepts('json'))379 this.halt(500, showExceptions.renderJSON(this, err))383 this.respond(500, showExceptions.renderJSON(this, err))384 else if (this.accepts('text'))381 this.halt(500, showExceptions.renderText(this, err))385 this.respond(500, showExceptions.renderText(this, err))386 else383 this.halt(500)387 this.respond(500)388 else385 this.halt(500)389 this.respond(500)390 if (set('throw exceptions'))391 throw err392 return this393 },
5 unmodified lines
399 * will then be called regardless of any settings that400 * may be present.401 *402 * notFound(function(){399 * this.halt(404, 'Sorry your page cannot be found')403 * this.respond(404, 'Sorry your page cannot be found')404 * })405 *406 * When "html" is accepted, and "helpful 404" is enabled407 * the not-found page will be shown, otherwise the
6 unmodified lines
414 notFound: function() {415 if (Express.notFound)416 Express.notFound.call(this)417 else if (this.accepts('html') && set('helpful 404'))414 this.halt(404, notFound.render(this))418 this.respond(404, notFound.render(this))419 else416 this.halt()420 this.respond()421 return this422 }423})
modified:spec/spec.plugins.hooks.js+1 added-1 removed
modified:spec/spec.plugins.session.js+1 added-1 removed
15 unmodified lines
16 end17 18 describe 'and requesting /favicon.ico'19 it 'should not set sid cookie'20 get('/favicon.ico', function(){ this.halt() })20 get('/favicon.ico', function(){ this.respond() })21 get('/favicon.ico').headers.should.not.have_property 'set-cookie'22 end23 end24 end
116 unmodified lines
modified:spec/spec.plugins.view.js+1 added-1 removed
79 unmodified lines
80 it 'should be passed the rendered content'81 get('/', function(){82 this.render('hello.html.haml', {}, function(err, content){83 if (err) this.error(err)84 else this.halt(203, content)84 else this.respond(203, content)85 })86 })87 get('/').body.should.include '<html>\n<body>'88 get('/').status.should.eql 203
116 unmodified lines
modified:spec/spec.request.js+2 added-28 removed
111 unmodified lines
112 end113 end114 end115 116 describe '#halt()'117 describe 'when given no arguments'118 it 'should respond with 404 Not Found'119 get('/user', function(){ this.halt() })120 get('/user').status.should.eql 404121 get('/user').body.should.include('Not Found')122 end123 end124125 describe 'when given a status code'126 it 'should respond with that status and its associated default body'127   get('/user', function(){ this.halt(400) })128   get('/user').status.should.eql 400129   get('/user').body.should.include('Bad Request')130 end131 end132133 describe 'when given a status code and body'134 it 'should respond with the status and its body'135 get('/user', function(){ this.halt(400, 'Oh noes!') })136 get('/user').status.should.eql 400137 get('/user').body.should.include('Oh noes!')138 end139 end140 end141116 describe '#contentType()'117 it 'should set Content-Type header with mime type passed'118 get('/style.css', function(){119 this.contentType('css')
118 unmodified lines
238 var err239 disable('throw exceptions')240 global.error(function(e){241 err = e268 this.halt(500, 'FAIL!')242 this.respond(500, 'FAIL!')243 })244 get('/', function(){ this.error(new Error('whoop')) })245 get('/').body.should.eql 'FAIL!'246 err.message.should.eql 'whoop'
101 unmodified lines
348 describe '#notFound()'349 describe 'when a notFound route is defined'350 it 'should be called'351 notFound(function(){378 this.halt(404, 'Sorry your page was not found')352 this.respond(404, 'Sorry your page was not found')353 })354 get('/', function(){ this.notFound() })355 get('/').body.should.eql 'Sorry your page was not found'356 end
38 unmodified lines