Added Express.Break
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent b1b5daf · Browse files · 4 files·+29 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+29 added−15 removed
- tests:+8 added−6 removed
- other:+21 added−9 removed
Provenance
- Commit
52241a1- Tree
dc15c94- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
b1b5daf
4 files changed+29 added−15 removed
modified:lib/express.core.js+21 added-9 removed
2 unmodified lines3 Express = { 4 version : '0.0.1',5 routes : [],6 7+ // --- Break8+ 9+ Break : function(){10+ this.toString = function() {11+ return 'Express.Break'12+ }13+ },14+ 15 // --- Response16 17 response : {18 headers : {},7 unmodified lines26 maxAge : 360027 },28 route : {29 callback : function() {22- Express.respond('Not Found')30+ Express.respond('Page or file cannot be found', 'Not Found')31 }32 }33 },34 70 unmodified lines105 })106 },107 108 fail : function(e) {109+ if (e.constructor == Express.Break) return110 this.header('Content-Type', 'text/html')102- this.response.body = this.respond(e.name + ': ' + e.message, 500)111+ this.response.body = e.message ? e.name + ': ' + e.message : e.toString()112+ this.response.status = 500113 },114 115 server : {116 56 unmodified lines173 /**174 * Redirect to _url_.175 *176 * @param {string} url167- * @return {string}177 * @api public178 */179 180 redirect : function(url) {181 // TODO: respond / redirect should halt182 this.header('Location', url)174- return this.respond('Moved Temporarily')183+ this.respond('Moved Temporarily')184 },185 186 /**187 * Set response status to _value_. Where value may be5 unmodified lines193 * @api public194 */195 196 status : function(value) {197+ if (value == undefined) throw 'Express.status(): value must be passed'198 if (typeof value == 'number') return this.response.status = value199 for (var code in node.http.STATUS_CODES)200 if (node.http.STATUS_CODES.hasOwnProperty(code))201 if (node.http.STATUS_CODES[code].toLowerCase() == value.toLowerCase())4 unmodified lines206 * Set response _status_ and _body_.207 *208 * @param {string} body209 * @param {string, int} status200- * @return {string}210 * @api public211 */212 213 respond : function(body, status) {205- this.status(status || body)206- return body214+ this.response.body = body215+ this.status(status)216+ throw new Express.Break()217 },218 219 /**220 * Sets _name_ header to _value_. When _value_359 unmodified lines580 'list of articles'581 })582 583 get(/articles\/(\d+)/, function(){584+ redirect('http://google.com') // TODO: remove / fix585 header('Content-Type', 'text/html')586 'article id ' + captures[1]576- redirect('http://google.com') // TODO: remove / fix587 })588 589 get('articles/:name', function(){580- if (param('name') == 'tj')590+ if (param('name') == 'tj') {591+ respond('Halted execution')592 'hello tj'593+ }594 else595 'article name ' + param('name')596 })597 1 unmodified linemodified:spec/spec.core.js+5 added-3 removed
152 unmodified linesExpand all15315415515615715815715815916016116216316316416516616716816917026 unmodified lines152 unmodified linesExpand all after_each Express.response.status = 200 end it 'should set response status and return body' Express.respond('Not Found').should.eql 'Not Found' it 'should set response status and body' -{ Express.respond('Page or file cannot be found', 'Not Found') }.should.throw_error Express.response.status.should.eql 404 Express.response.body.should.eql 'Page or file cannot be found' end it 'should allow specific status to be passed' Express.respond('File cannot be found', 404).should.eql 'File cannot be found' -{ Express.respond('File cannot be found', 404) }.should.throw_error Express.response.status.should.eql 404 Express.response.body.should.eql 'File cannot be found' end end describe '.hashToArray()'26 unmodified linesmodified:spec/spec.dom.html+2 added-2 removed
1 <html>2 <head>3- <link type="text/css" rel="stylesheet" href="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.4.2/lib/jspec.css" />4- <script src="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.4.2/lib/jspec.js"></script>3+ <link type="text/css" rel="stylesheet" href="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.5.0/lib/jspec.css" />4+ <script src="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.5.0/lib/jspec.js"></script>5 <script src="../lib/express.core.js"></script>6 <script src="../lib/express.mime.js"></script>7 <script src="../lib/express.view.js"></script>8 <script>17 unmodified linesmodified:spec/spec.rhino.js+1 added-1 removed
1 2-load('/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.4.2/lib/jspec.js')2+load('/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.5.0/lib/jspec.js')3 load('lib/express.core.js')4 load('lib/express.mime.js')5 load('lib/express.view.js')6 7 unmodified lines