No need for anonymous scope
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 7936439 · Browse files · 3 files·+104 added−107 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+104 added−107 removed
- tests:+3 added−2 removed
- other:+101 added−105 removed
Provenance
- Commit
19104a0- Tree
405f67f- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
7936439
3 files changed+104 added−107 removed
modified:lib/express/core.js+101 added-105 removed
1234564789101112131415161718192021222324252627282930313233343536373839404154344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878867891011121314151617181920212223242526919227282930313233943435969736373839100101102103104105106404142434410810911011111211311411511645464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
// Express - Core - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
;(function(){ process.mixin(GLOBAL, require('sys'))process.mixin(GLOBAL, require('sys')) // --- Route Route = Class({ 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){ 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, 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) puts('Express started at http://' + this.host + ':' + this.port + '/') }, route: function(request, response){ this.request = request, this.response = response if (typeof (this.response.body = this.router.route(request)) == 'string') this.respond() }, respond: function(body){ if (body) this.response.body = body this.response.sendHeader(200, this.response.headers) this.response.sendBody(this.response.body || '') this.response.finish() } }) // --- Express Express = { version: '0.0.1', routes: [], helpers: {}, server: new Server// --- Route
Route = Class({ 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){ this.request = request return this.matchingRoute().run() }, // --- Utilities matchingRoute: function(){ for (var i = 0, len = Express.routes.length; i < len; ++i) if (this.match(Express.routes[i])) return Express.routes[i] }, /** * Return a routing function for _method_. * Check if _route_ matches the current request. * * @param {string} method * @return {function} * @param {object} route * @return {bool} * @api private */ function route(method) { return function(path, options, fn){ if (options instanceof Function) fn = options, options = {} Express.routes.push(new Route(method, path, fn, options)) }
match: function(route) { if (this.request.method == route.method) return route.path == this.request.uri.path } // --- Routing API get = view = route('get') post = create = route('post') del = destroy = route('delete') put = update = route('put') })()No newline at end of file})
// --- Server
Server = Class({ port: 3000, host: 'localhost', router: new Router, 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) puts('Express started at http://' + this.host + ':' + this.port + '/') }, route: function(request, response){ this.request = request, this.response = response if (typeof (this.response.body = this.router.route(request)) == 'string') this.respond() }, respond: function(body){ if (body) this.response.body = body this.response.sendHeader(200, this.response.headers) this.response.sendBody(this.response.body || '') this.response.finish() }})
// --- Express
Express = { version: '0.0.1', routes: [], helpers: {}, server: new Server}
// --- Helpers /** * Return a routing function for _method_. * * @param {string} method * @return {function} * @api private */
function route(method) { return function(path, options, fn){ if (options instanceof Function) fn = options, options = {} Express.routes.push(new Route(method, path, fn, options)) }}
// --- Routing API
get = view = route('get')post = create = route('post')del = destroy = route('delete')put = update = route('put')modified:spec/spec.core.routing.js+2 added-2 removed
15 unmodified lines161718192020212222232425262715 unmodified lines describe 'with options and callback function' it 'should respond with a body string' this[method]('/user', {}, function(){ return 'test' return 'test with options' }) this[method]('/user').body.should.eql 'test' this[method]('/user').body.should.eql 'test with options' end end endendmodified:spec/spec.node.js+1 added
1234567891021 unmodified linesrequire.paths.unshift("./spec/lib", "./lib");process.mixin(GLOBAL, require("sys"))
require("jspec")require("express")require("express/spec")
var posix = require('posix')
quit = process.exit21 unmodified lines