Asynchronous handlers working.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by csausdev · committed by csausdev · parent 0a911dd · Browse files · 4 files·+23 added−18 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+23 added−18 removed
- tests:+1 added−0 removed
- other:+22 added−18 removed
Provenance
- Commit
80328f9- Tree
cd5a3fd- Author
csausdev <gareth.jones@sensis.com.au>- Committer
csausdev <gareth.jones@sensis.com.au>- Parent
0a911dd
4 files changed+23 added−18 removed
modified:examples/simple.js+1 added-1 removed
123445678require.paths.unshift("./lib")require('express')
get('hello/:value', function(req, res) {get('hello/:value', function() { '<html><head><title>Traditional Example</title></head><body><h1>Hello '+param('value')+'</h1></body></html>' });
Express.start();modified:lib/express.core.js+17 added-8 removed
249 unmodified lines250 Express.home = Express.settings.basepath251 Express.response.body = null252 Express.response.status = 200253 Express.request = request254+ Express.originalResponse = response255 request.headers = Express.arrayToHash(request.headers)256 request.uri.params = Express.parseNestedParams(request.uri.params)257 request.uri.path = Express.normalizePath(request.uri.path)258 Express.hook('onRequest')259 try { Express.response.body = Express.callRouteFor(request) }260 catch (e) { Express.fail(e) }260- Express.hook('onResponse')261- response.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))262- response.sendBody(Express.response.body || '')263- response.finish()261+ 262+ if (Express.response.body) {263+ Express.server.finished()264+ }265 })265- }266+ },267+ 268+ finished: function(body) {269+ if (body) Express.response.body = body270+ Express.hook('onResponse')271+ Express.originalResponse.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))272+ Express.originalResponse.sendBody(Express.response.body || '')273+ Express.originalResponse.finish()274+ }275+ 276 },277 278 /**279 * Fail Express with exception _e_.373 unmodified lines653 if (route.keys)654 for (var i = 0, len = route.keys.length; i < len; ++i) 655 this.request.uri.params[route.keys[i]] = Express.captures[i+1] || null656 var body = this.eval(Express.contentsOf(route.callback))647- if (typeof body != 'string') 648- throw "route `" + route.method.toUpperCase() + ' ' + route.path + "' must return a string"657+ if (typeof body != 'string') body = null658 return body659 },660 661 /**653- * Attemp to find and return a route matching _request_.662+ * Attempt to find and return a route matching _request_.663 *664 * @param {object} request665 * @return {object}666 * @api private68 unmodified linesmodified:spec/spec.node.js+1 added
25 unmodified lines26272829303132333425 unmodified lines .exec('spec/spec.mime.js') .exec('spec/spec.cookie.js') .exec('spec/spec.session.js') .exec('spec/spec.view.js') .exec('spec/spec.async.js')}JSpec.run({ formatter : JSpec.formatters.Terminal, failuresOnly : true })JSpec.report()
modified:Readme.md+4 added-9 removed
24 unmodified lines252627282930293233343536303132333435363738463940414243444585 unmodified lines13113213313414013524 unmodified lines * param key substitution * pre-conditions * overidding * etc ## Limitations
Note that the current version does not cope with url handlers that do not return a string.So, any asynchronous operations made in your handler won't get executed properly (i.e. the handler will return,terminating the http response, before your callbacks return). This will be fixed very soon - I have a dodgy hack coded up that works,but is far too embarrassing to show the world.
## Performance
Extensive benchmarking will wait until a development version has been released. However for simple an average route and response body Ruby's Sinatra scored **1454** request per second using Thin, while Express scored **2762**.
## Examples
See examples/simple.js for a working server. Just type "node examples/simple.js" and then go to http://localhost:3000/hello/worldSee examples/simple.js for a working server that uses a synchronous handler. Just type "node examples/simple.js" and then go to http://localhost:3000/hello/worldFor an asynchronous example, run "node examples/async.js" and then go to http://localhost:3000/hello/world/2000
GET /user/tj/edit get('user/:name/:operation', function(){85 unmodified linesMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.No newline at end of fileSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.