Renamed Request#halt() to Request#respond(). Closes #289
Before adoption: this commit came in with the history the project adopted, so no landing records it.
deprecated Request#halt(), however it will remain until 1.0
authored by visionmedia · committed by visionmedia · parent 0352b97 · 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
Provenance
- Commit
b90a3db- Tree
eaeadbe- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
0352b97
10 files changed+50 added−72 removed
modified:examples/chat/app.js+3 added-3 removed
40 unmodified lines41424344454546474849505152535455555657585913 unmodified lines73747576777778798040 unmodified lines messages .push(utils.escape(this.param('name')) + ': ' + utils.escape(this.param('message')) .replace(/(http:\/\/[^\s]+)/g, '<a href="$1" target="express-chat">$1</a>') .replace(/:\)/g, '<img src="http://icons3.iconfinder.netdna-cdn.com/data/icons/ledicons/emoticon_smile.png">')) this.halt(200) this.respond(200)})
get('/chat/messages', function(){ var self = this, previousLength = messages.length, timer = setInterval(function(){ if (messages.length > previousLength) self.contentType('json'), previousLength = messages.length, self.halt(200, JSON.encode(messages)), self.respond(200, JSON.encode(messages)), clearInterval(timer) }, 100)})
13 unmodified lines return 'Hello :)'})
get('/favicon.ico', function(){ this.halt() this.notFound()})
run()No newline at end of fileNo newline at end of filemodified:lib/express/plugins/redirect.js+1 added-1 removed
40 unmodified lines4142434445454647484916 unmodified lines40 unmodified lines
redirect: function(url, code) { if (url === 'back' || url === 'home') url = this[url] this.header('Location', url) this.halt(code || 303) this.respond(code || 303) } }) } },16 unmodified linesmodified:lib/express/plugins/static.js+1 added-1 removed
63 unmodified lines64 : 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 linesmodified:lib/express/plugins/view.js+2 added-2 removed
121 unmodified lines122 * 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 lines163 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 linesmodified:lib/express/core.js+1 added-1 removed
278 unmodified lines279 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 linesmodified:lib/express/request.js+37 added-33 removed
9 unmodified lines10 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')16 17 // --- Helpers18 19 /**132 unmodified lines152 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 */182 172- 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} stream137 unmodified lines350 * 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 lines377 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 lines399 * 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 lines414 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
21 unmodified lines2223242526262728293016 unmodified lines21 unmodified lines end it 'should be able to halt the request' GLOBAL.before(function(){ this.halt(404, 'woo!') this.respond(404, 'woo!') }) get('/user', function(){ return 'fail' }) get('/user').status.should.eql 404 get('/user').body.should.eql 'woo!'16 unmodified linesmodified:spec/spec.plugins.session.js+1 added-1 removed
15 unmodified lines16 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 end116 unmodified linesmodified:spec/spec.plugins.view.js+1 added-1 removed
79 unmodified lines80 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 203116 unmodified linesmodified:spec/spec.request.js+2 added-28 removed
111 unmodified lines112 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- end124-125- 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- end132-133- 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- end141-116 describe '#contentType()'117 it 'should set Content-Type header with mime type passed'118 get('/style.css', function(){119 this.contentType('css')118 unmodified lines238 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 lines348 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 end38 unmodified lines