Merge branch 'plugin-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 4d1bda0, 4907701 · Browse files · 3 files·+21 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+21 added−8 removed
- tests:+13 added−1 removed
- other:+8 added−7 removed
Provenance
- Commit
1879648- Tree
ae3a109- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
4d1bda0- Parent 2
4907701
3 files changed+21 added−8 removed
modified:lib/express/plugins/hooks.js+7 added-7 removed
12345456789101112131514151617182 unmodified lines212223242625262728294 unmodified lines34353637393839404142434 unmodified lines48495051525253545556575859606162626364656667
// Express - Hooks - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
var before = [], after = []exports.callbacks = { before: [], after: [] }
/** * Add a _fn_ to be excuted before a request. * * @param {function} fn * @api public */
exports.before = function(fn) { before.push(fn) exports.callbacks.before.push(fn)}
/** * Add a _fn_ to be excuted after a request.2 unmodified lines * @api public */
exports.after = function(fn) { after.push(fn) exports.callbacks.after.push(fn)}
// --- Hooks
4 unmodified lines * Initialize extensions. */ init: function() { process.mixin(GLOBAL, exports) process.mixin(GLOBAL, { before: exports.before, after: exports.after }) } }, // --- Events4 unmodified lines * Invoke all before handlers. */ request: function(event) { $(before).each(function(fn){ $(exports.callbacks.before).each(function(fn){ fn.call(event.request, event.request) }) }, /** * Invoke all after handlers. */ response: function(event) { $(after).each(function(fn){ $(exports.callbacks.after).each(function(fn){ fn.call(event.request, event.request) }) } }})modified:lib/express/core.js+1 added
267 unmodified lines268 269 route: function(request, response){270 request = new Request(request, response)271 request.trigger('request')272+ if (request.response.finished) return273 try {274 if (typeof (body = (new Router(request)).route()) == 'string')275 request.halt(200, body)276 }46 unmodified linesmodified:spec/spec.plugins.hooks.js+13 added-1 removed
12345567891011126 unmodified lines192021222324252627282930313233343511 unmodified lines
describe 'Express' before_each reset() use(require('express/plugins/hooks').Hooks) hooks = require('express/plugins/hooks') use(hooks.Hooks) hooks.callbacks.before = [] hooks.callbacks.after = [] end describe 'Hooks' describe 'before()'6 unmodified lines }) get('/user', function(){}) get('/user').body.should.eql 'foobar' end it 'should be able to halt the request' GLOBAL.before(function(){ this.halt(404, 'woo!') }) get('/user', function(){ return 'fail' }) get('/user').status.should.eql 404 get('/user').body.should.eql 'woo!' end end describe 'after()' it 'should be called after every request'11 unmodified lines