Added simple router
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 07aa40b · Browse files · 2 files·+31 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−6 removed
- other:+31 added−6 removed
Provenance
- Commit
7936439- Tree
0df46a4- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
07aa40b
2 files changed+31 added−6 removed
modified:examples/simple.js+3 added-3 removed
123456567899
require.paths.unshift("./lib")require('express')
get('/i/like/:object', function() { 'You like "' + param('object') + '"'get('/hello', function() { return 'Whats up!'})
Express.server.start()Express.server.run()modified:lib/express/core.js+28 added-3 removed
10 unmodified lines1112131415161718192021222324222526272829303132333435363738394041424344454647484950515253543255565758596061626364656616 unmodified lines8384858687888990918 unmodified lines100101102103791041051061071088 unmodified lines10 unmodified lines init: function(method, path, fn, options){ this.method = method this.path = path this.fn = fn }, run: function(){ return eval('with(Express.helpers){ (' + this.fn + ')() }') } }) // --- Router Router = Class({ route: function(request){ return 'test' this.request = request return this.matchingRoute().run() }, matchingRoute: function(){ for (var i = 0, len = Express.routes.length; i < len; ++i) if (this.match(Express.routes[i])) return Express.routes[i] }, /** * Check if _route_ matches the current request. * * @param {object} route * @return {bool} * @api private */
match: function(route) { if (this.request.method == route.method) return route.path == this.request.uri.path } }) // --- Server Server = Class({ port: 3000, host: 'localhost', router: new Router, start: function(host, port){ run: function(host, port){ var self = this if (host) this.host = host if (port) this.port = port require('http') .createServer(function(request, response){ response.headers = {} request.method = request.method.toLowerCase() request.addListener('body', function(chunk){ request.body += chunk }) request.addListener('complete', function(){ self.route(request, response) }) }) .listen(this.port, this.host)16 unmodified lines Express = { version: '0.0.1', routes: [], helpers: {}, server: new Server } // --- Utilities8 unmodified lines function route(method) { return function(path, options, fn){ if (options instanceof Function) callback = options, options = {} fn = options, options = {} Express.routes.push(new Route(method, path, fn, options)) } } 8 unmodified lines