Compare commits

...

55 Commits
2.2.2 ... 2.3.1

Author SHA1 Message Date
Tj Holowaychuk
2572a78648 Release 2.3.1 2011-04-26 15:26:04 -07:00
Tj Holowaychuk
470774cfba Fixed template caching collision issue. Closes #644
the parent view is resolved first so we always have the absolute path
available, so this should prevent collisions.
2011-04-26 15:24:31 -07:00
Tj Holowaychuk
351f6abe4c Merge branch 'refactor/router' 2011-04-26 14:36:51 -07:00
Tj Holowaychuk
53a16e1795 Added app.match() as app.match.all() 2011-04-26 13:59:59 -07:00
Tj Holowaychuk
ff77c8b205 Added app.lookup() as app.lookup.all() 2011-04-26 13:59:37 -07:00
Tj Holowaychuk
b33f38b109 Added app.remove() for app.remove.all() 2011-04-26 13:58:43 -07:00
Tj Holowaychuk
b9596d7ce8 Added app.remove.VERB() 2011-04-26 13:57:05 -07:00
Tj Holowaychuk
251175c025 test for previous commit 2011-04-26 13:30:47 -07:00
Tj Holowaychuk
fda1bc4630 moved fn.params to route.params 2011-04-26 13:30:21 -07:00
Tj Holowaychuk
83c2c176a9 misc 2011-04-26 13:28:20 -07:00
Tj Holowaychuk
9be5992f22 misc refactoring 2011-04-26 13:26:49 -07:00
Tj Holowaychuk
d8d23c0bf8 misc 2011-04-26 13:21:19 -07:00
Tj Holowaychuk
b2689fc40e docs 2011-04-26 13:16:19 -07:00
Tj Holowaychuk
a4cfde350f moved more tests 2011-04-26 13:14:21 -07:00
Tj Holowaychuk
7374027457 added router.test.js 2011-04-26 13:13:07 -07:00
Tj Holowaychuk
63328c2177 added .index to match() retvals 2011-04-26 13:01:53 -07:00
Tj Holowaychuk
c4e2ce23e5 more Route tests 2011-04-26 12:59:25 -07:00
Tj Holowaychuk
dacad53b2e Added instancoef Route test 2011-04-26 12:57:14 -07:00
Tj Holowaychuk
4ffd5280a7 expose Route 2011-04-26 12:55:31 -07:00
Tj Holowaychuk
74310fb464 misc refactoring 2011-04-26 12:53:57 -07:00
Tj Holowaychuk
8b2268cf38 Started Route implementation 2011-04-26 12:41:05 -07:00
Tj Holowaychuk
fb655f4981 added lib/router/methods.js 2011-04-26 11:49:51 -07:00
Tj Holowaychuk
7208c33d72 refactored view.js 2011-04-26 11:40:44 -07:00
Tj Holowaychuk
4efb25d048 refactored https.js 2011-04-26 11:39:09 -07:00
Tj Holowaychuk
a3678cd7f6 refactored http.js 2011-04-26 11:34:41 -07:00
Tj Holowaychuk
393d38f1ab stubbed Route 2011-04-26 11:31:31 -07:00
Tj Holowaychuk
805b9ac3a9 docs for "view cache" setting 2011-04-25 12:06:20 -07:00
Tj Holowaychuk
379b9812be refactored options() helper 2011-04-25 10:26:08 -07:00
Tj Holowaychuk
a9992b5647 docs 2011-04-25 10:19:13 -07:00
Tj Holowaychuk
b6c0a9b1b5 moved router to router/index 2011-04-25 10:17:13 -07:00
Tj Holowaychuk
1d2dd2a375 Merge branch 'feature/router' 2011-04-25 10:13:46 -07:00
Tj Holowaychuk
63db694aa2 utilizing local router 2011-04-25 10:13:30 -07:00
Tj Holowaychuk
b6aca36ad9 htmlEscape -> escape 2011-04-25 10:11:38 -07:00
Tj Holowaychuk
8420ae93fd removed old path utils 2011-04-25 10:10:43 -07:00
Tj Holowaychuk
6722716fa7 Moved router over from connect 2011-04-25 10:08:54 -07:00
Tj Holowaychuk
658e064220 Release 2.3.0 2011-04-25 09:49:54 -07:00
Tj Holowaychuk
66b472e567 connect >= 1.4.0 2011-04-25 09:32:57 -07:00
Tj Holowaychuk
3d242a607e Fixed caching of views when using several apps. Closes #637
simple fix :)
2011-04-25 09:25:23 -07:00
Tj Holowaychuk
bc68e5e7a3 misc 2011-04-25 09:20:31 -07:00
Tj Holowaychuk
aa6858189c misc refactor 2011-04-23 18:54:49 -07:00
Tj Holowaychuk
06fda62c9e Merge branch 'refactor/route-middleware' 2011-04-22 16:49:04 -07:00
Tj Holowaychuk
5688ea650d Fixed gotcha invoking app.param() callbacks once per route middleware. Closes #638 2011-04-22 16:48:54 -07:00
Tj Holowaychuk
f5c4e9d612 Updated connect submodule 2011-04-22 16:34:42 -07:00
Tj Holowaychuk
b9eda2a59d Updated connect submodule 2011-04-22 16:11:13 -07:00
Tj Holowaychuk
8c168b0971 connect >= 1.3.1 < 2.0.0 2011-04-22 16:11:07 -07:00
Tj Holowaychuk
9c20a50ee2 Added failing test for gotcha 2011-04-22 14:44:08 -07:00
Tj Holowaychuk
eac666574e viewHelpers -> _locals 2011-04-21 08:26:30 -07:00
Tj Holowaychuk
e4c840f6b8 Added res.helpers() as alias of res.locals()
to match app.locals() / app.helpers()
2011-04-20 15:26:22 -07:00
Daniel Shaw
3afbcd0acf JSON and JSONP default to UTF-8 in the same way as HTML. Closes #632.
Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
2011-04-20 09:00:02 -07:00
Daniel Shaw
8f054dbcaf JSON and JSONP default to UTF-8 in the same way as HTML. Introduces app.set('charset') to set charset default at the application level. Closes #632.
Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
2011-04-20 09:00:02 -07:00
Tj Holowaychuk
ccc39e5aa2 Fixed partial lookup precedence. Closes #631 2011-04-19 10:23:16 -07:00
Tj Holowaychuk
53d4da2a9c Added failing partial precedence test 2011-04-19 10:19:45 -07:00
Tj Holowaychuk
d9e7153fc9 Renamed "cache views" to "view cache". Closes #628 2011-04-17 16:42:03 -07:00
Tj Holowaychuk
dc02b0d5ae Added options support to res.clearCookie() 2011-04-17 16:37:16 -07:00
Aaron Heckmann
e0bc5711b8 auto set Content-Type in res.attachement
Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
2011-04-14 13:48:22 -07:00
22 changed files with 861 additions and 325 deletions

View File

@@ -1,4 +1,28 @@
2.3.1 / 2011-04-26
==================
* Added `app.match()` as `app.match.all()`
* Added `app.lookup()` as `app.lookup.all()`
* Added `app.remove()` for `app.remove.all()`
* Added `app.remove.VERB()`
* Fixed template caching collision issue. Closes #644
* Moved router over from connect and started refactor
2.3.0 / 2011-04-25
==================
* Added options support to `res.clearCookie()`
* Added `res.helpers()` as alias of `res.locals()`
* Added; json defaults to UTF-8 with `res.send()`. Closes #632. [Daniel * Dependency `connect >= 1.4.0`
* Changed; auto set Content-Type in res.attachement [Aaron Heckmann]
* Renamed "cache views" to "view cache". Closes #628
* Fixed caching of views when using several apps. Closes #637
* Fixed gotcha invoking `app.param()` callbacks once per route middleware.
Closes #638
* Fixed partial lookup precedence. Closes #631
Shaw]
2.2.2 / 2011-04-12
==================

View File

@@ -11,7 +11,7 @@ var fs = require('fs')
* Framework version.
*/
var version = '2.2.2';
var version = '2.3.0';
/**
* Add session support.

View File

@@ -82,6 +82,7 @@ Express supports the following settings out of the box:
* _views_ Root views directory defaulting to **CWD/views**
* _view engine_ Default view engine name for views rendered without extensions
* _view options_ An object specifying global view options
* _view cache_ Enable view caching (enabled in production)
### Routing
@@ -894,7 +895,7 @@ To parse incoming _Cookie_ headers, use the _cookieParser_ middleware, which pro
// use req.cookies.rememberme
});
### res.clearCookie(name)
### res.clearCookie(name[, options])
Clear cookie _name_ by setting "expires" far in the past.

View File

@@ -11,7 +11,8 @@
var connect = require('connect')
, HTTPSServer = require('./https')
, HTTPServer = require('./http');
, HTTPServer = require('./http')
, Route = require('./router/route')
/**
* Re-export connect auto-loaders.
@@ -27,7 +28,7 @@ var exports = module.exports = connect.middleware;
* Framework version.
*/
exports.version = '2.2.2';
exports.version = '2.3.1';
/**
* Shortcut for `new Server(...)`.
@@ -51,6 +52,7 @@ exports.createServer = function(options){
exports.HTTPServer = HTTPServer;
exports.HTTPSServer = HTTPSServer;
exports.Route = Route;
/**
* View extensions.

View File

@@ -11,12 +11,24 @@
var qs = require('qs')
, connect = require('connect')
, router = connect.router
, router = require('./router')
, methods = router.methods.concat(['del', 'all'])
, view = require('./view')
, url = require('url')
, utils = connect.utils;
/**
* Expose `HTTPServer`.
*/
exports = module.exports = HTTPServer;
/**
* Server proto.
*/
var app = HTTPServer.prototype;
/**
* Initialize a new `HTTPServer` with optional `middleware`.
*
@@ -24,7 +36,7 @@ var qs = require('qs')
* @api public
*/
var Server = exports = module.exports = function HTTPServer(middleware){
function HTTPServer(middleware){
connect.HTTPServer.call(this, []);
this.init(middleware);
};
@@ -33,7 +45,7 @@ var Server = exports = module.exports = function HTTPServer(middleware){
* Inherit from `connect.HTTPServer`.
*/
Server.prototype.__proto__ = connect.HTTPServer.prototype;
app.__proto__ = connect.HTTPServer.prototype;
/**
* Initialize the server.
@@ -42,21 +54,17 @@ Server.prototype.__proto__ = connect.HTTPServer.prototype;
* @api private
*/
Server.prototype.init = function(middleware){
app.init = function(middleware){
var self = this;
this.match = {};
this.lookup = {};
this.cache = {};
this.settings = {};
this.redirects = {};
this.isCallbacks = {};
this.viewHelpers = {};
this._locals = {};
this.dynamicViewHelpers = {};
this.errorHandlers = [];
// default "home" to /
this.set('home', '/');
// set "env" to NODE_ENV, defaulting to "development"
this.set('env', process.env.NODE_ENV || 'development');
// expose objects to each other
@@ -98,7 +106,7 @@ Server.prototype.init = function(middleware){
// default production configuration
this.configure('production', function(){
this.enable('cache views');
this.enable('view cache');
});
// register error handlers on "listening"
@@ -106,6 +114,18 @@ Server.prototype.init = function(middleware){
this.on('listening', this.registerErrorHandlers.bind(this));
// route lookup methods
this.remove = function(url){
return self.remove.all(url);
};
this.match = function(url){
return self.match.all(url);
};
this.lookup = function(url){
return self.lookup.all(url);
};
methods.forEach(function(method){
self.match[method] = function(url){
return self.router.match(url, 'all' == method
@@ -113,6 +133,12 @@ Server.prototype.init = function(middleware){
: method);
};
self.remove[method] = function(url){
return self.router.remove(url, 'all' == method
? null
: method);
};
self.lookup[method] = function(path){
return self.router.lookup(path, 'all' == method
? null
@@ -125,7 +151,7 @@ Server.prototype.init = function(middleware){
* When using the vhost() middleware register error handlers.
*/
Server.prototype.onvhost = function(){
app.onvhost = function(){
this.registerErrorHandlers();
};
@@ -136,7 +162,7 @@ Server.prototype.onvhost = function(){
* @api public
*/
Server.prototype.registerErrorHandlers = function(){
app.registerErrorHandlers = function(){
this.errorHandlers.forEach(function(fn){
this.use(function(err, req, res, next){
fn.apply(this, arguments);
@@ -155,7 +181,7 @@ Server.prototype.registerErrorHandlers = function(){
* @api public
*/
Server.prototype.use = function(route, middleware){
app.use = function(route, middleware){
var app, home, handle;
if ('string' != typeof route) {
@@ -213,7 +239,7 @@ Server.prototype.use = function(route, middleware){
* @api public
*/
Server.prototype.mounted = function(fn){
app.mounted = function(fn){
this.__mounted = fn;
return this;
};
@@ -225,7 +251,7 @@ Server.prototype.mounted = function(fn){
* @api public
*/
Server.prototype.register = function(){
app.register = function(){
view.register.apply(this, arguments);
return this;
};
@@ -239,9 +265,9 @@ Server.prototype.register = function(){
* @api public
*/
Server.prototype.helpers =
Server.prototype.locals = function(obj){
utils.merge(this.viewHelpers, obj);
app.helpers =
app.locals = function(obj){
utils.merge(this._locals, obj);
return this;
};
@@ -254,7 +280,7 @@ Server.prototype.locals = function(obj){
* @api public
*/
Server.prototype.dynamicHelpers = function(obj){
app.dynamicHelpers = function(obj){
utils.merge(this.dynamicViewHelpers, obj);
return this;
};
@@ -302,7 +328,7 @@ Server.prototype.dynamicHelpers = function(obj){
* @api public
*/
Server.prototype.param = function(name, fn){
app.param = function(name, fn){
if (Array.isArray(name)) {
name.forEach(function(name){
this.param(name, fn);
@@ -323,7 +349,7 @@ Server.prototype.param = function(name, fn){
* @api public
*/
Server.prototype.error = function(fn){
app.error = function(fn){
this.errorHandlers.push(fn);
return this;
};
@@ -337,7 +363,7 @@ Server.prototype.error = function(fn){
* @api public
*/
Server.prototype.is = function(type, fn){
app.is = function(type, fn){
if (!fn) return this.isCallbacks[type];
this.isCallbacks[type] = fn;
return this;
@@ -353,7 +379,7 @@ Server.prototype.is = function(type, fn){
* @api public
*/
Server.prototype.set = function(setting, val){
app.set = function(setting, val){
if (val === undefined) {
if (this.settings.hasOwnProperty(setting)) {
return this.settings[setting];
@@ -374,7 +400,7 @@ Server.prototype.set = function(setting, val){
* @api public
*/
Server.prototype.enabled = function(setting){
app.enabled = function(setting){
return !!this.set(setting);
};
@@ -386,7 +412,7 @@ Server.prototype.enabled = function(setting){
* @api public
*/
Server.prototype.disabled = function(setting){
app.disabled = function(setting){
return !this.set(setting);
};
@@ -398,7 +424,7 @@ Server.prototype.disabled = function(setting){
* @api public
*/
Server.prototype.enable = function(setting){
app.enable = function(setting){
return this.set(setting, true);
};
@@ -410,7 +436,7 @@ Server.prototype.enable = function(setting){
* @api public
*/
Server.prototype.disable = function(setting){
app.disable = function(setting){
return this.set(setting, false);
};
@@ -423,7 +449,7 @@ Server.prototype.disable = function(setting){
* @api public
*/
Server.prototype.redirect = function(key, url){
app.redirect = function(key, url){
this.redirects[key] = url;
return this;
};
@@ -437,7 +463,7 @@ Server.prototype.redirect = function(key, url){
* @api public
*/
Server.prototype.configure = function(env, fn){
app.configure = function(env, fn){
if ('function' == typeof env) {
fn = env, env = 'all';
}
@@ -450,7 +476,7 @@ Server.prototype.configure = function(env, fn){
// Generate routing methods
function generateMethod(method){
Server.prototype[method] = function(path, fn){
app[method] = function(path){
var self = this;
// Lookup
@@ -461,27 +487,10 @@ function generateMethod(method){
}
// Ensure router is mounted
if (!this.__usedRouter) {
this.use(this.router);
}
// Route specific middleware support
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 1);
fn = args.pop();
(function stack(middleware){
middleware.forEach(function(fn){
if (Array.isArray(fn)) {
stack(fn);
} else {
self[method](path, fn);
}
});
})(args);
}
if (!this.__usedRouter) this.use(this.router);
// Generate the route
this.routes[method](path, fn);
this.routes[method].apply(this, arguments);
return this;
};
return arguments.callee;
@@ -491,4 +500,4 @@ methods.forEach(generateMethod);
// Alias delete as "del"
Server.prototype.del = Server.prototype.delete;
app.del = app.delete;

View File

@@ -13,6 +13,18 @@ var connect = require('connect')
, HTTPServer = require('./http')
, https = require('https');
/**
* Expose `HTTPSServer`.
*/
exports = module.exports = HTTPSServer;
/**
* Server proto.
*/
var app = HTTPSServer.prototype;
/**
* Initialize a new `HTTPSServer` with the
* given `options`, and optional `middleware`.
@@ -22,7 +34,7 @@ var connect = require('connect')
* @api public
*/
var Server = exports = module.exports = function HTTPSServer(options, middleware){
function HTTPSServer(options, middleware){
connect.HTTPSServer.call(this, options, []);
this.init(middleware);
};
@@ -31,10 +43,10 @@ var Server = exports = module.exports = function HTTPSServer(options, middleware
* Inherit from `connect.HTTPSServer`.
*/
Server.prototype.__proto__ = connect.HTTPSServer.prototype;
app.__proto__ = connect.HTTPSServer.prototype;
// mixin HTTPServer methods
Object.keys(HTTPServer.prototype).forEach(function(method){
Server.prototype[method] = HTTPServer.prototype[method];
app[method] = HTTPServer.prototype[method];
});

View File

@@ -197,7 +197,7 @@ req.flash = function(type, msg){
, args = arguments
, formatters = this.app.flashFormatters || {};
formatters.__proto__ = flashFormatters;
msg = utils.miniMarkdown(utils.htmlEscape(msg));
msg = utils.miniMarkdown(utils.escape(msg));
msg = msg.replace(/%([a-zA-Z])/g, function(_, format){
var formatter = formatters[format];
if (formatter) return formatter(args[i++]);

View File

@@ -75,10 +75,12 @@ res.send = function(body, headers, status){
}
} else {
if (!this.header('Content-Type')) {
this.charset = this.charset || 'utf-8';
this.contentType('.json');
}
body = JSON.stringify(body);
if (this.req.query.callback && this.app.set('jsonp callback')) {
this.charset = this.charset || 'utf-8';
this.header('Content-Type', 'text/javascript');
body = this.req.query.callback.replace(/[^\w$.]/g, '') + '(' + body + ');';
}
@@ -176,6 +178,7 @@ res.contentType = function(type){
*/
res.attachment = function(filename){
if (filename) this.contentType(filename);
this.header('Content-Disposition', filename
? 'attachment; filename="' + path.basename(filename) + '"'
: 'attachment');
@@ -211,7 +214,7 @@ res.download = function(path, filename, fn, fn2){
if (err) {
if (!sentHeader) self.removeHeader('Content-Disposition');
if (sentHeader) {
if (fn2) fn2(err);
fn2 && fn2(err);
} else if (fn) {
fn(err);
} else {
@@ -245,11 +248,15 @@ res.header = function(name, val){
* Clear cookie `name`.
*
* @param {String} name
* @param {Object} options
* @api public
*/
res.clearCookie = function(name){
this.cookie(name, '', { expires: new Date(1) });
res.clearCookie = function(name, options){
var opts = { expires: new Date(1) };
this.cookie(name, '', options
? utils.merge(options, opts)
: opts);
};
/**
@@ -404,7 +411,8 @@ res.local = function(name, val){
* @api public
*/
res.locals = function(obj){
res.locals =
res.helpers = function(obj){
if (obj) {
for (var key in obj) {
this.local(key, obj[key]);

329
lib/router/index.js Normal file
View File

@@ -0,0 +1,329 @@
/*!
* Express - router
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var utils = require('../utils')
, parse = require('url').parse
, _methods = require('./methods')
, Route = require('./route');
/**
* Expose router.
*/
exports = module.exports = router;
/**
* Expose methods.
*/
exports.methods = _methods;
/**
* Provides Sinatra-like routing capabilities.
*
* @param {Function} fn
* @return {Function}
* @api private
*/
function router(fn){
var self = this
, methods = {}
, routes = {}
, params = {};
if (!fn) throw new Error('router provider requires a callback function');
// Generate method functions
_methods.forEach(function(method){
methods[method] = generateMethodFunction(method.toUpperCase());
});
// Alias del -> delete
methods.del = methods.delete;
// Apply callback to all methods
methods.all = function(){
var args = arguments;
_methods.forEach(function(name){
methods[name].apply(this, args);
});
return self;
};
// Register param callback
methods.param = function(name, fn){
params[name] = fn;
};
fn.call(this, methods);
function generateMethodFunction(name) {
var localRoutes = routes[name] = routes[name] || [];
return function(path, fn){
var keys = []
, middleware = [];
// slice middleware
if (arguments.length > 2) {
middleware = Array.prototype.slice.call(arguments, 1, arguments.length);
fn = middleware.pop();
middleware = utils.flatten(middleware);
}
fn.middleware = middleware;
if (!path) throw new Error(name + ' route requires a path');
if (!fn) throw new Error(name + ' route ' + path + ' requires a callback');
var route = new Route(name, path, fn);
localRoutes.push(route);
return self;
};
}
function router(req, res, next){
var route
, self = this;
(function pass(i){
if (route = match(req, routes, i)) {
var i = 0
, keys = route.keys;
req.params = route.params;
// Param preconditions
(function param(err) {
try {
var key = keys[i++]
, val = req.params[key]
, fn = params[key];
if ('route' == err) {
pass(req._route_index + 1);
// Error
} else if (err) {
next(err);
// Param has callback
} else if (fn) {
// Return style
if (1 == fn.length) {
req.params[key] = fn(val);
param();
// Middleware style
} else {
fn(req, res, param, val);
}
// Finished processing params
} else if (!key) {
// route middleware
i = 0;
(function nextMiddleware(err){
var fn = route.callback.middleware[i++];
if ('route' == err) {
pass(req._route_index + 1);
} else if (err) {
next(err);
} else if (fn) {
fn(req, res, nextMiddleware);
} else {
route.callback.call(self, req, res, function(err){
if (err) {
next(err);
} else {
pass(req._route_index + 1);
}
});
}
})();
// More params
} else {
param();
}
} catch (err) {
next(err);
}
})();
} else if ('OPTIONS' == req.method) {
options(req, res, routes);
} else {
next();
}
})();
};
router.remove = function(path, method, ret){
var ret = ret || []
, route;
// method specific remove
if (method) {
method = method.toUpperCase();
if (routes[method]) {
for (var i = 0; i < routes[method].length; ++i) {
route = routes[method][i];
if (path == route.path) {
route.index = i;
routes[method].splice(i, 1);
ret.push(route);
--i;
}
}
}
// global remove
} else {
_methods.forEach(function(method){
router.remove(path, method, ret);
});
}
return ret;
};
router.lookup = function(path, method, ret){
ret = ret || [];
// method specific lookup
if (method) {
method = method.toUpperCase();
if (routes[method]) {
routes[method].forEach(function(route, i){
if (path == route.path) {
route.index = i;
ret.push(route);
}
});
}
// global lookup
} else {
_methods.forEach(function(method){
router.lookup(path, method, ret);
});
}
return ret;
};
router.match = function(url, method, ret){
var ret = ret || []
, i = 0
, route
, req;
// method specific matches
if (method) {
method = method.toUpperCase();
req = { url: url, method: method };
while (route = match(req, routes, i)) {
i = req._route_index + 1;
route.index = i;
ret.push(route);
}
// global matches
} else {
_methods.forEach(function(method){
router.match(url, method, ret);
});
}
return ret;
};
return router;
}
/**
* Respond to OPTIONS.
*
* @param {ServerRequest} req
* @param {ServerResponse} req
* @param {Array} routes
* @api private
*/
function options(req, res, routes) {
var pathname = parse(req.url).pathname
, body = optionsFor(pathname, routes).join(',');
res.send(body, { Allow: body });
}
/**
* Return OPTIONS array for the given `path`, matching `routes`.
*
* @param {String} path
* @param {Array} routes
* @return {Array}
* @api private
*/
function optionsFor(path, routes) {
return _methods.filter(function(method){
var arr = routes[method.toUpperCase()];
for (var i = 0, len = arr.length; i < len; ++i) {
if (arr[i].regexp.test(path)) return true;
}
}).map(function(method){
return method.toUpperCase();
});
}
/**
* Attempt to match the given request to
* one of the routes. When successful
* a route function is returned.
*
* @param {ServerRequest} req
* @param {Object} routes
* @return {Function}
* @api private
*/
function match(req, routes, i) {
var captures
, method = req.method
, i = i || 0;
// pass HEAD to GET routes
if ('HEAD' == method) method = 'GET';
// routes for this method
if (routes = routes[method]) {
var url = parse(req.url)
, pathname = url.pathname;
// matching routes
for (var len = routes.length; i < len; ++i) {
var route = routes[i]
, fn = route.callback
, path = route.regexp
, keys = route.keys;
// match
if (captures = path.exec(pathname)) {
route.params = [];
for (var j = 1, l = captures.length; j < l; ++j) {
var key = keys[j-1],
val = 'string' == typeof captures[j]
? decodeURIComponent(captures[j])
: captures[j];
if (key) {
route.params[key] = val;
} else {
route.params.push(val);
}
}
req._route_index = i;
return route;
}
}
}
}

31
lib/router/methods.js Normal file
View File

@@ -0,0 +1,31 @@
/*!
* Express - router - methods
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Supported HTTP / WebDAV methods.
*/
module.exports = [
'get'
, 'post'
, 'put'
, 'delete'
, 'connect'
, 'options'
, 'trace'
, 'copy'
, 'lock'
, 'mkcol'
, 'move'
, 'propfind'
, 'proppatch'
, 'unlock'
, 'report'
, 'mkactivity'
, 'checkout'
, 'merge'
];

64
lib/router/route.js Normal file
View File

@@ -0,0 +1,64 @@
/*!
* Express - router - Route
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Expose `Route`.
*/
module.exports = Route;
/**
* Initialize `Route` with the given HTTP `method`, `path`,
* and callback `fn`.
*
* @param {String} method
* @param {String} path
* @param {Function} fn
* @api private
*/
function Route(method, path, fn) {
this.callback = fn;
this.path = path;
this.regexp = normalize(path, this.keys = []);
this.method = method;
}
/**
* Normalize the given path string,
* returning a regular expression.
*
* An empty array should be passed,
* which will contain the placeholder
* key names. For example "/user/:id" will
* then contain ["id"].
*
* @param {String|RegExp} path
* @param {Array} keys
* @return {RegExp}
* @api private
*/
function normalize(path, keys) {
if (path instanceof RegExp) return path;
path = path
.concat('/?')
.replace(/\/\(/g, '(?:/')
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
keys.push(key);
slash = slash || '';
return ''
+ (optional ? '' : slash)
+ '(?:'
+ (optional ? slash : '')
+ (format || '') + (capture || '([^/]+?)') + ')'
+ (optional || '');
})
.replace(/([\/.])/g, '\\$1')
.replace(/\*/g, '(.+)');
return new RegExp('^' + path + '$', 'i');
}

View File

@@ -5,64 +5,6 @@
* MIT Licensed
*/
/**
* Module dependencies.
*/
var path = require('path')
, basename = path.basename
, dirname = path.dirname
, extname = path.extname;
/**
* Memory cache.
*/
var cache = {
basename: {}
, dirname: {}
, extname: {}
};
/**
* Cached basename.
*
* @param {String} path
* @return {String}
* @api private
*/
exports.basename = function(path){
return cache.basename[path]
|| (cache.basename[path] = basename(path));
};
/**
* Cached dirname.
*
* @param {String} path
* @return {String}
* @api private
*/
exports.dirname = function(path){
return cache.dirname[path]
|| (cache.dirname[path] = dirname(path));
};
/**
* Cached extname.
*
* @param {String} path
* @return {String}
* @api private
*/
exports.extname = function(path){
return cache.extname[path]
|| (cache.extname[path] = extname(path));
};
/**
* Merge object `b` with `a` giving precedence to
* values in object `a`.
@@ -88,6 +30,27 @@ exports.union = function(a, b){
return a;
};
/**
* Flatten the given `arr`.
*
* @param {Array} arr
* @return {Array}
* @api private
*/
exports.flatten = function(arr, ret){
var ret = ret || []
, len = arr.length;
for (var i = 0; i < len; ++i) {
if (Array.isArray(arr[i])) {
exports.flatten(arr[i], ret);
} else {
ret.push(arr[i]);
}
}
return ret;
};
/**
* Parse mini markdown implementation.
* The following conversions are supported,
@@ -117,7 +80,7 @@ exports.miniMarkdown = function(str){
* @api private
*/
exports.htmlEscape = function(html) {
exports.escape = function(html) {
return String(html)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')

View File

@@ -21,14 +21,6 @@ var path = require('path')
, http = require('http')
, res = http.ServerResponse.prototype;
/**
* Memory cache.
*
* @type Object
*/
var cache = {};
/**
* Expose constructors.
*/
@@ -261,12 +253,15 @@ res._render = function(view, opts, fn, parent, sub){
var options = {}
, self = this
, app = this.app
, helpers = app.viewHelpers
, helpers = app._locals
, dynamicHelpers = app.dynamicViewHelpers
, viewOptions = app.set('view options')
, cacheViews = app.set('cache views')
, cacheViews = app.enabled('view cache')
, root = app.set('views') || process.cwd() + '/views';
// cache id
var cid = view + (parent ? ':' + parent.path : '');
// merge "view options"
if (viewOptions) merge(options, viewOptions);
@@ -331,15 +326,18 @@ res._render = function(view, opts, fn, parent, sub){
};
// cached view
if (cache[view]) {
view = cache[view];
if (app.cache[cid]) {
view = app.cache[cid];
options.filename = view.path;
// resolve view
} else {
var orig = view = new View(view, options);
// Try _ prefix ex: ./views/_<name>.jade
if (!view.exists) view = new View(orig.prefixPath, options);
if (partial) {
view = new View(orig.prefixPath, options);
if (!view.exists) view = orig;
}
// Try index ex: ./views/user/index.jade
if (!view.exists) view = new View(orig.indexPath, options);
@@ -365,7 +363,7 @@ res._render = function(view, opts, fn, parent, sub){
options.filename = view.path;
var engine = view.templateEngine;
view.fn = engine.compile(view.contents, options)
if (cacheViews) cache[orig.view] = view;
if (cacheViews) app.cache[cid] = view;
}
// layout helper

View File

@@ -9,13 +9,19 @@
* Module dependencies.
*/
var utils = require('../utils')
, extname = utils.extname
, dirname = utils.dirname
, basename = utils.basename
var path = require('path')
, extname = path.extname
, dirname = path.dirname
, basename = path.basename
, fs = require('fs')
, stat = fs.statSync;
/**
* Expose `View`.
*/
exports = module.exports = View;
/**
* Require cache.
*/
@@ -30,7 +36,7 @@ var cache = {};
* @api private
*/
var View = exports = module.exports = function View(view, options) {
function View(view, options) {
options = options || {};
this.view = view;
this.root = options.root;

View File

@@ -1,7 +1,7 @@
{
"name": "express",
"description": "Sinatra inspired web development framework",
"version": "2.2.2",
"version": "2.3.1",
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"contributors": [
{ "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" },
@@ -10,7 +10,7 @@
{ "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
],
"dependencies": {
"connect": ">= 1.3.0 < 2.0.0",
"connect": ">= 1.4.0 < 2.0.0",
"mime": ">= 0.0.1",
"qs": ">= 0.0.6"
},

View File

@@ -6,7 +6,8 @@
var express = require('express')
, connect = require('connect')
, assert = require('assert')
, should = require('should');
, should = require('should')
, Route = express.Route;
module.exports = {
'test inheritance': function(){
@@ -17,6 +18,7 @@ module.exports = {
'test constructor exports': function(){
express.should.have.property('HTTPServer');
express.should.have.property('HTTPSServer');
express.should.have.property('Route');
},
'test connect middleware autoloaders': function(){
@@ -433,175 +435,5 @@ module.exports = {
{ url: '/' },
{ body: 'restored' }
);
},
'test route middleware': function(){
var app = express.createServer();
function allow(role) {
return function(req, res, next) {
// this is totally not real, dont use this :)
// for tests only
if (req.headers['x-role'] == role) {
next();
} else {
res.send(401);
}
}
}
function restrictAge(age) {
return function(req, res, next){
if (req.headers['x-age'] >= age) {
next();
} else {
res.send(403);
}
}
}
app.get('/xxx', allow('member'), restrictAge(18), function(req, res){
res.send(200);
});
app.get('/booze', [allow('member')], restrictAge(18), function(req, res){
res.send(200);
});
app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){
res.send(200);
});
['xxx', 'booze', 'tobi'].forEach(function(thing){
assert.response(app,
{ url: '/' + thing },
{ body: 'Unauthorized', status: 401 });
assert.response(app,
{ url: '/' + thing, headers: { 'X-Role': 'member' }},
{ body: 'Forbidden', status: 403 });
assert.response(app,
{ url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},
{ body: 'OK', status: 200 });
});
},
'test named capture groups': function(){
var app = express.createServer();
app.get('/user/:id([0-9]{2,10})', function(req, res){
res.send('user ' + req.params.id);
});
assert.response(app,
{ url: '/user/12' },
{ body: 'user 12' });
assert.response(app,
{ url: '/user/ab' },
{ body: 'Cannot GET /user/ab' });
},
'test .param()': function(){
var app = express.createServer();
var users = [
{ name: 'tj' }
, { name: 'tobi' }
, { name: 'loki' }
, { name: 'jane' }
, { name: 'bandit' }
];
function integer(n){ return parseInt(n, 10); };
app.param(['to', 'from'], integer);
app.param('user', function(req, res, next, id){
if (req.user = users[id]) {
next();
} else {
next(new Error('failed to find user'));
}
});
app.get('/user/:user', function(req, res, next){
res.send('user ' + req.user.name);
});
app.get('/users/:from-:to', function(req, res, next){
var names = users.slice(req.params.from, req.params.to).map(function(user){
return user.name;
});
res.send('users ' + names.join(', '));
});
assert.response(app,
{ url: '/user/0' },
{ body: 'user tj' });
assert.response(app,
{ url: '/user/1' },
{ body: 'user tobi' });
assert.response(app,
{ url: '/users/0-3' },
{ body: 'users tj, tobi, loki' });
},
'test OPTIONS': function(){
var app = express.createServer();
app.get('/', function(){});
app.get('/user/:id', function(){});
app.put('/user/:id', function(){});
assert.response(app,
{ url: '/', method: 'OPTIONS' },
{ headers: { Allow: 'GET' }});
assert.response(app,
{ url: '/user/12', method: 'OPTIONS' },
{ headers: { Allow: 'GET,PUT' }});
},
'test app.lookup': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user/:id', function(){});
app.get('/user/:id/:op?', function(){});
app.put('/user/:id', function(){});
app.get('/user/:id/edit', function(){});
app.get('/user').should.have.length(1);
app.get('/user/:id').should.have.length(1);
app.get('/user/:id/:op?').should.have.length(1);
app.put('/user/:id').should.have.length(1);
app.get('/user/:id/edit').should.have.length(1);
app.get('/').should.have.be.empty;
app.all('/user/:id').should.have.length(2);
app.lookup.get('/user').should.have.length(1);
app.lookup.get('/user/:id').should.have.length(1);
app.lookup.get('/user/:id/:op?').should.have.length(1);
app.lookup.put('/user/:id').should.have.length(1);
app.lookup.get('/user/:id/edit').should.have.length(1);
app.lookup.get('/').should.have.be.empty;
app.lookup.all('/user/:id').should.have.length(2);
},
'test app.match': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user/:id', function(){});
app.get('/user/:id/:op?', function(){});
app.put('/user/:id', function(){});
app.get('/user/:id/edit', function(){});
app.match.get('/user').should.have.length(1);
app.match.get('/user/12').should.have.length(2);
app.match.get('/user/12/:op?').should.have.length(1);
app.match.put('/user/100').should.have.length(1);
app.match.get('/user/5/edit').should.have.length(2);
app.match.get('/').should.have.be.empty;
app.match.all('/user/123').should.have.length(3);
}
};

1
test/fixtures/_foobar.jade vendored Normal file
View File

@@ -0,0 +1 @@
p two

1
test/fixtures/foobar.jade vendored Normal file
View File

@@ -0,0 +1 @@
p one

View File

@@ -61,7 +61,7 @@ module.exports = {
assert.response(app,
{ url: '/bool' },
{ body: 'true'
, headers: { 'Content-Type': 'application/json' }});
, headers: { 'Content-Type': 'application/json; charset=utf-8' }});
assert.response(app,
{ url: '/html' },
@@ -76,7 +76,7 @@ module.exports = {
{ body: '{"foo":"bar"}'
, status: 201
, headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json; charset=utf-8'
, 'X-Foo': 'baz'
}});
@@ -85,7 +85,7 @@ module.exports = {
{ body: 'test({"foo":"bar"});'
, status: 201
, headers: {
'Content-Type': 'text/javascript'
'Content-Type': 'text/javascript; charset=utf-8'
, 'X-Foo': 'baz'
}});
@@ -93,7 +93,7 @@ module.exports = {
{ url: '/jsonp?callback=baz' },
{ body: 'baz({"foo":"bar"});'
, status: 201, headers: {
'Content-Type': 'text/javascript'
'Content-Type': 'text/javascript; charset=utf-8'
, 'X-Foo': 'baz'
}});
@@ -102,7 +102,7 @@ module.exports = {
{ body: 'invalid({"foo":"bar"});'
, status: 201
, headers: {
'Content-Type': 'text/javascript'
'Content-Type': 'text/javascript; charset=utf-8'
, 'X-Foo': 'baz'
}});
@@ -111,7 +111,7 @@ module.exports = {
{ body: '{"foo":"bar"}'
, status: 201
, headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json; charset=utf-8'
, 'X-Foo': 'baz'
}});
@@ -207,12 +207,14 @@ module.exports = {
assert.response(app,
{ url: '/javascripts/jquery.js' },
{ body: 'whatever'
, headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});
, headers: { 'Content-Type': 'application/javascript'
, 'Content-Disposition': 'attachment; filename="jquery.js"' }});
assert.response(app,
{ url: '/style.css' },
{ body: 'some stylezzz'
, headers: { 'Content-Disposition': 'attachment' }});
, headers: { 'Content-Type': 'text/html; charset=utf-8'
, 'Content-Disposition': 'attachment' }});
},
'test #redirect()': function(){
@@ -568,7 +570,7 @@ module.exports = {
var app = express.createServer();
app.get('/', function(req, res){
res.clearCookie('rememberme');
res.clearCookie('rememberme', { path: '/foo' });
res.redirect('/');
});
@@ -576,7 +578,7 @@ module.exports = {
{ url: '/' },
function(res){
res.headers['set-cookie']
.should.eql(['rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT']);
.should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);
});
},

245
test/router.test.js Normal file
View File

@@ -0,0 +1,245 @@
/**
* Module dependencies.
*/
var express = require('express')
, connect = require('connect')
, assert = require('assert')
, should = require('should')
, Route = express.Route;
module.exports = {
'test route middleware': function(beforeExit){
var app = express.createServer()
, calls = 0;
function allow(role) {
return function(req, res, next) {
// this is totally not real, dont use this :)
// for tests only
if (req.headers['x-role'] == role) {
next();
} else {
res.send(401);
}
}
}
function restrictAge(age) {
return function(req, res, next){
if (req.headers['x-age'] >= age) {
next();
} else {
res.send(403);
}
}
}
app.param('user', function(req, res, next, user){
++calls;
next();
});
app.get('/xxx', allow('member'), restrictAge(18), function(req, res){
res.send(200);
});
app.get('/booze', [allow('member')], restrictAge(18), function(req, res){
res.send(200);
});
app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){
res.send(200);
});
app.get('/user/:user', [allow('member')], [[restrictAge(18)]], function(req, res){
res.send(200);
});
['xxx', 'booze', 'tobi', 'user/tj'].forEach(function(thing){
assert.response(app,
{ url: '/' + thing },
{ body: 'Unauthorized', status: 401 });
assert.response(app,
{ url: '/' + thing, headers: { 'X-Role': 'member' }},
{ body: 'Forbidden', status: 403 });
assert.response(app,
{ url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},
{ body: 'OK', status: 200 });
});
beforeExit(function(){
calls.should.equal(3);
});
},
'test named capture groups': function(){
var app = express.createServer();
app.get('/user/:id([0-9]{2,10})', function(req, res){
res.send('user ' + req.params.id);
});
assert.response(app,
{ url: '/user/12' },
{ body: 'user 12' });
assert.response(app,
{ url: '/user/ab' },
{ body: 'Cannot GET /user/ab' });
},
'test .param()': function(){
var app = express.createServer();
var users = [
{ name: 'tj' }
, { name: 'tobi' }
, { name: 'loki' }
, { name: 'jane' }
, { name: 'bandit' }
];
function integer(n){ return parseInt(n, 10); };
app.param(['to', 'from'], integer);
app.param('user', function(req, res, next, id){
if (req.user = users[id]) {
next();
} else {
next(new Error('failed to find user'));
}
});
app.get('/user/:user', function(req, res, next){
res.send('user ' + req.user.name);
});
app.get('/users/:from-:to', function(req, res, next){
var names = users.slice(req.params.from, req.params.to).map(function(user){
return user.name;
});
res.send('users ' + names.join(', '));
});
assert.response(app,
{ url: '/user/0' },
{ body: 'user tj' });
assert.response(app,
{ url: '/user/1' },
{ body: 'user tobi' });
assert.response(app,
{ url: '/users/0-3' },
{ body: 'users tj, tobi, loki' });
},
'test OPTIONS': function(){
var app = express.createServer();
app.get('/', function(){});
app.get('/user/:id', function(){});
app.put('/user/:id', function(){});
assert.response(app,
{ url: '/', method: 'OPTIONS' },
{ headers: { Allow: 'GET' }});
assert.response(app,
{ url: '/user/12', method: 'OPTIONS' },
{ headers: { Allow: 'GET,PUT' }});
},
'test app.lookup': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user/:id', function(){});
app.get('/user/:id/:op?', function(){});
app.put('/user/:id', function(){});
app.get('/user/:id/edit', function(){});
var route = app.get('/user/:id')[0]
route.should.be.an.instanceof(Route);
route.callback.should.be.a('function');
route.path.should.equal('/user/:id');
route.regexp.should.be.an.instanceof(RegExp);
route.method.should.equal('GET');
route.index.should.equal(1);
route.keys.should.eql(['id']);
app.get('/user').should.have.length(1);
app.get('/user/:id').should.have.length(1);
app.get('/user/:id/:op?').should.have.length(1);
app.put('/user/:id').should.have.length(1);
app.get('/user/:id/edit').should.have.length(1);
app.get('/').should.have.be.empty;
app.all('/user/:id').should.have.length(2);
app.lookup.get('/user').should.have.length(1);
app.lookup.get('/user/:id').should.have.length(1);
app.lookup.get('/user/:id/:op?').should.have.length(1);
app.lookup.put('/user/:id').should.have.length(1);
app.lookup.get('/user/:id/edit').should.have.length(1);
app.lookup.get('/').should.have.be.empty;
app.lookup.all('/user/:id').should.have.length(2);
app.lookup('/user/:id').should.have.length(2);
},
'test app.remove': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user', function(){});
app.put('/user', function(){});
app.get('/user').should.have.length(2);
var removed = app.remove.get('/user');
removed.should.have.length(2);
var removed = app.remove.get('/user');
removed.should.have.length(0);
app.get('/user').should.have.length(0);
app.get('/user/:id', function(){});
app.put('/user/:id', function(){});
app.del('/user/:id', function(){});
app.remove.all('/user/:id').should.have.length(3);
app.remove.all('/user/:id').should.have.length(0);
app.get('/user/:id', function(){});
app.put('/user/:id', function(){});
app.del('/user/:id', function(){});
app.remove('/user/:id').should.have.length(3);
},
'test app.match': function(){
var app = express.createServer();
app.get('/user', function(){});
app.get('/user/:id', function(){});
app.get('/user/:id/:op?', function(){});
app.put('/user/:id', function(){});
app.get('/user/:id/edit', function(){});
var route = app.match.get('/user/12')[0];
route.should.be.an.instanceof(Route);
route.callback.should.be.a('function');
route.path.should.equal('/user/:id');
route.regexp.should.be.an.instanceof(RegExp);
route.method.should.equal('GET');
route.index.should.equal(2);
route.keys.should.eql(['id']);
route.params.id.should.equal('12');
app.match.get('/user').should.have.length(1);
app.match.get('/user/12').should.have.length(2);
app.match.get('/user/12/:op?').should.have.length(1);
app.match.put('/user/100').should.have.length(1);
app.match.get('/user/5/edit').should.have.length(2);
app.match.get('/').should.have.be.empty;
app.match.all('/user/123').should.have.length(3);
app.match('/user/123').should.have.length(3);
}
};

View File

@@ -657,6 +657,14 @@ module.exports = {
assert.response(app,
{ url: '/error' },
{ status: 500 });
app.get('/underscore', function(req, res, next){
res.partial('foobar');
});
assert.response(app,
{ url: '/underscore' },
{ body: '<p>two</p>' });
},
'test #partial() with several calls': function(){