mirror of
https://github.com/expressjs/express.git
synced 2026-02-27 03:07:33 +00:00
Compare commits
114 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc185a8c0e | ||
|
|
ae1078944c | ||
|
|
385a05dd10 | ||
|
|
2572a78648 | ||
|
|
470774cfba | ||
|
|
351f6abe4c | ||
|
|
53a16e1795 | ||
|
|
ff77c8b205 | ||
|
|
b33f38b109 | ||
|
|
b9596d7ce8 | ||
|
|
251175c025 | ||
|
|
fda1bc4630 | ||
|
|
83c2c176a9 | ||
|
|
9be5992f22 | ||
|
|
d8d23c0bf8 | ||
|
|
b2689fc40e | ||
|
|
a4cfde350f | ||
|
|
7374027457 | ||
|
|
63328c2177 | ||
|
|
c4e2ce23e5 | ||
|
|
dacad53b2e | ||
|
|
4ffd5280a7 | ||
|
|
74310fb464 | ||
|
|
8b2268cf38 | ||
|
|
fb655f4981 | ||
|
|
7208c33d72 | ||
|
|
4efb25d048 | ||
|
|
a3678cd7f6 | ||
|
|
393d38f1ab | ||
|
|
805b9ac3a9 | ||
|
|
379b9812be | ||
|
|
a9992b5647 | ||
|
|
b6c0a9b1b5 | ||
|
|
1d2dd2a375 | ||
|
|
63db694aa2 | ||
|
|
b6aca36ad9 | ||
|
|
8420ae93fd | ||
|
|
6722716fa7 | ||
|
|
658e064220 | ||
|
|
66b472e567 | ||
|
|
3d242a607e | ||
|
|
bc68e5e7a3 | ||
|
|
aa6858189c | ||
|
|
06fda62c9e | ||
|
|
5688ea650d | ||
|
|
f5c4e9d612 | ||
|
|
b9eda2a59d | ||
|
|
8c168b0971 | ||
|
|
9c20a50ee2 | ||
|
|
eac666574e | ||
|
|
e4c840f6b8 | ||
|
|
3afbcd0acf | ||
|
|
8f054dbcaf | ||
|
|
ccc39e5aa2 | ||
|
|
53d4da2a9c | ||
|
|
d9e7153fc9 | ||
|
|
dc02b0d5ae | ||
|
|
e0bc5711b8 | ||
|
|
957cf45fa1 | ||
|
|
f4487343df | ||
|
|
ca1bdb31e3 | ||
|
|
236a412459 | ||
|
|
759a57bdb6 | ||
|
|
1abb674a07 | ||
|
|
961146a287 | ||
|
|
573f940985 | ||
|
|
882916bb2e | ||
|
|
47d1c62732 | ||
|
|
d39293c025 | ||
|
|
2942dafdfd | ||
|
|
4e1aefa5b5 | ||
|
|
89383ecc57 | ||
|
|
08046f7692 | ||
|
|
885fb1fa92 | ||
|
|
6a58c71528 | ||
|
|
371d66ba2a | ||
|
|
25bddf3fb5 | ||
|
|
f6e9fb13f8 | ||
|
|
f0df8434e7 | ||
|
|
e4d3f239e5 | ||
|
|
bcc22dfa6f | ||
|
|
f614709a01 | ||
|
|
11250d22c9 | ||
|
|
4b4de29725 | ||
|
|
99b49d2718 | ||
|
|
6230ec55be | ||
|
|
0733d3c585 | ||
|
|
6f8370ff0e | ||
|
|
bc244ed07e | ||
|
|
41266aa8e4 | ||
|
|
45faee3e5b | ||
|
|
74ff735e10 | ||
|
|
97879f2b16 | ||
|
|
1a338251ad | ||
|
|
354dc768c1 | ||
|
|
8a0796cd94 | ||
|
|
cbc3b26584 | ||
|
|
d628583db8 | ||
|
|
058777be1e | ||
|
|
260d03a0c4 | ||
|
|
6dcf6f41cc | ||
|
|
799f790886 | ||
|
|
cb3c4b0ea9 | ||
|
|
798d255ba6 | ||
|
|
28ba9e8ac5 | ||
|
|
7888cb0506 | ||
|
|
5e284a20cc | ||
|
|
770357e727 | ||
|
|
673ba22555 | ||
|
|
fb38d9cfb7 | ||
|
|
dda89a57ec | ||
|
|
62df63d3a0 | ||
|
|
e71696cf34 | ||
|
|
b5d8d58670 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,5 +6,7 @@ lib-cov
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.swp
|
||||
*.swo
|
||||
benchmarks/graphs
|
||||
testing.js
|
||||
|
||||
64
History.md
64
History.md
@@ -1,4 +1,68 @@
|
||||
|
||||
2.3.2 / 2011-04-27
|
||||
==================
|
||||
|
||||
* Fixed view hints
|
||||
|
||||
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
|
||||
==================
|
||||
|
||||
* Added second callback support for `res.download()` connection errors
|
||||
* Fixed `filename` option passing to template engine
|
||||
|
||||
2.2.1 / 2011-04-04
|
||||
==================
|
||||
|
||||
* Added `layout(path)` helper to change the layout within a view. Closes #610
|
||||
* Fixed `partial()` collection object support.
|
||||
Previously only anything with `.length` would work.
|
||||
When `.length` is present one must still be aware of holes,
|
||||
however now `{ collection: {foo: 'bar'}}` is valid, exposes
|
||||
`keyInCollection` and `keysInCollection`.
|
||||
|
||||
* Performance improved with better view caching
|
||||
* Removed `request` and `response` locals
|
||||
* Changed; errorHandler page title is now `Express` instead of `Connect`
|
||||
|
||||
2.2.0 / 2011-03-30
|
||||
==================
|
||||
|
||||
* Added `app.lookup.VERB()`, ex `app.lookup.put('/user/:id')`. Closes #606
|
||||
* Added `app.match.VERB()`, ex `app.match.put('/user/12')`. Closes #606
|
||||
* Added `app.VERB(path)` as alias of `app.lookup.VERB()`.
|
||||
* Dependency `connect >= 1.2.0`
|
||||
|
||||
2.1.1 / 2011-03-29
|
||||
==================
|
||||
|
||||
* Added; expose `err.view` object when failing to locate a view
|
||||
* Fixed `res.partial()` call `next(err)` when no callback is given [reported by aheckmann]
|
||||
* Fixed; `res.send(undefined)` responds with 204 [aheckmann]
|
||||
|
||||
2.1.0 / 2011-03-24
|
||||
==================
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ The following are the major contributors of Express (in no specific order).
|
||||
|
||||
## More Information
|
||||
|
||||
* [express-expose](http://github.com/visionmedia/express-expose) expose objects, functions, modules and more to client-side js with ease
|
||||
* [express-configure](http://github.com/visionmedia/express-configuration) async configuration support
|
||||
* [express-messages](http://github.com/visionmedia/express-messages) flash notification rendering helper
|
||||
* [express-namespace](http://github.com/visionmedia/express-namespace) namespaced route support
|
||||
|
||||
16
bin/express
16
bin/express
@@ -11,7 +11,7 @@ var fs = require('fs')
|
||||
* Framework version.
|
||||
*/
|
||||
|
||||
var version = '2.1.0';
|
||||
var version = '2.3.2';
|
||||
|
||||
/**
|
||||
* Add session support.
|
||||
@@ -37,14 +37,14 @@ var templateEngine = 'jade';
|
||||
|
||||
var usage = ''
|
||||
+ '\n'
|
||||
+ ' \x1b[1mUsage\x1b[0m: express [options] [PATH]\n'
|
||||
+ ' Usage: express [options] [path]\n'
|
||||
+ '\n'
|
||||
+ ' \x1b[1mOptions\x1b[0m:\n'
|
||||
+ ' -s, --sessions Add session support\n'
|
||||
+ ' -t, --template ENGINE Add template ENGINE support (jade|ejs). Defaults to jade\n'
|
||||
+ ' -c, --css ENGINE Add stylesheet ENGINE support (less|sass|stylus). Defaults to plain css\n'
|
||||
+ ' -v, --version Output framework version\n'
|
||||
+ ' -h, --help Output help information\n'
|
||||
+ ' Options:\n'
|
||||
+ ' -s, --sessions add session support\n'
|
||||
+ ' -t, --template <engine> add template <engine> support (jade|ejs). default=jade\n'
|
||||
+ ' -c, --css <engine> add stylesheet <engine> support (less|sass|stylus). default=plain css\n'
|
||||
+ ' -v, --version output framework version\n'
|
||||
+ ' -h, --help output help information\n'
|
||||
;
|
||||
|
||||
/**
|
||||
|
||||
131
docs/guide.html
131
docs/guide.html
@@ -232,6 +232,8 @@
|
||||
<li><a href="#app.error()">error()</a></li>
|
||||
<li><a href="#app.helpers()">helpers()</a></li>
|
||||
<li><a href="#app.dynamichelpers()">dynamicHelpers()</a></li>
|
||||
<li><a href="#app.lookup">lookup</a></li>
|
||||
<li><a href="#app.match">match</a></li>
|
||||
<li><a href="#app.mounted()">mounted()</a></li>
|
||||
<li><a href="#app.register()">register()</a></li>
|
||||
<li><a href="#app.listen()">listen()</a></li>
|
||||
@@ -1222,7 +1224,7 @@ res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOn
|
||||
<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });
|
||||
</code></pre>
|
||||
|
||||
<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieDecoder</em> middleware, which provides the <em>req.cookies</em> object:</p>
|
||||
<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieParser</em> middleware, which provides the <em>req.cookies</em> object:</p>
|
||||
|
||||
<pre><code>app.use(express.cookieParser());
|
||||
|
||||
@@ -1506,21 +1508,30 @@ should call <em>next(err)</em> if it does not wish to deal with the exception:</
|
||||
|
||||
<p>Registers static view helpers.</p>
|
||||
|
||||
<p> app.helpers({</p>
|
||||
|
||||
<pre><code> name: function(first, last){ return first + ', ' + last }
|
||||
, firstName: 'tj'
|
||||
, lastName: 'holowaychuk'
|
||||
<pre><code>app.helpers({
|
||||
name: function(first, last){ return first + ', ' + last }
|
||||
, firstName: 'tj'
|
||||
, lastName: 'holowaychuk'
|
||||
});
|
||||
</code></pre>
|
||||
|
||||
<p> });</p>
|
||||
|
||||
<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,
|
||||
as well as the <em>name()</em> function exposed.</p>
|
||||
|
||||
<pre><code><%= name(firstName, lastName) %>
|
||||
</code></pre>
|
||||
|
||||
<p>Express also provides a few locals by default:</p>
|
||||
|
||||
<pre><code>- `settings` the app's settings object
|
||||
- `filename` the view's filename
|
||||
- `request` the request object
|
||||
- `response` the response object
|
||||
- `app` the application itself
|
||||
</code></pre>
|
||||
|
||||
<p>This method is aliased as <em>app.locals()</em>.</p>
|
||||
|
||||
<h3 id="app.dynamichelpers()">app.dynamicHelpers(obj)</h3>
|
||||
|
||||
<p>Registers dynamic view helpers. Dynamic view helpers
|
||||
@@ -1540,6 +1551,110 @@ becomes the local variable it is associated with.</p>
|
||||
<pre><code><%= session.name %>
|
||||
</code></pre>
|
||||
|
||||
<h3 id="app.lookup">app.lookup</h3>
|
||||
|
||||
<p> The <em>app.lookup</em> http methods returns an array of callback functions
|
||||
associated with the given <em>path</em>.</p>
|
||||
|
||||
<p> Suppose we define the following routes:</p>
|
||||
|
||||
<pre><code> app.get('/user/:id', function(){});
|
||||
app.put('/user/:id', function(){});
|
||||
app.get('/user/:id/:op?', function(){});
|
||||
</code></pre>
|
||||
|
||||
<p> We can utilize this lookup functionality to check which routes
|
||||
have been defined, which can be extremely useful for higher level
|
||||
frameworks built on Express.</p>
|
||||
|
||||
<pre><code> app.lookup.get('/user/:id');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.get('/user/:id/:op?');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.put('/user/:id');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.all('/user/:id');
|
||||
// => [Function, Function]
|
||||
|
||||
app.lookup.all('/hey');
|
||||
// => []
|
||||
</code></pre>
|
||||
|
||||
<p> To alias <em>app.lookup.VERB()</em>, we can simply invoke <em>app.VERB()</em>
|
||||
without a callback, as a shortcut, for example the following are
|
||||
equivalent:</p>
|
||||
|
||||
<pre><code> app.lookup.get('/user');
|
||||
app.get('/user');
|
||||
</code></pre>
|
||||
|
||||
<p> Each function returned has the following properties:</p>
|
||||
|
||||
<pre><code> var fn = app.get('/user/:id/:op?')[0];
|
||||
|
||||
fn.regexp
|
||||
// => /^\/user\/(?:([^\/]+?))(?:\/([^\/]+?))?\/?$/i
|
||||
|
||||
fn.keys
|
||||
// => ['id', 'op']
|
||||
|
||||
fn.path
|
||||
// => '/user/:id/:op?'
|
||||
|
||||
fn.method
|
||||
// => 'GET'
|
||||
</code></pre>
|
||||
|
||||
<h3 id="app.match">app.match</h3>
|
||||
|
||||
<p> The <em>app.match</em> http methods return an array of callback functions
|
||||
which match the given <em>url</em>, which may include a query string etc. This
|
||||
is useful when you want reflect on which routes have the opportunity to
|
||||
respond.</p>
|
||||
|
||||
<p> Suppose we define the following routes:</p>
|
||||
|
||||
<pre><code> app.get('/user/:id', function(){});
|
||||
app.put('/user/:id', function(){});
|
||||
app.get('/user/:id/:op?', function(){});
|
||||
</code></pre>
|
||||
|
||||
<p> Our match against <strong>GET</strong> will return two functions, since the <em>:op</em>
|
||||
in our second route is optional.</p>
|
||||
|
||||
<pre><code> app.match.get('/user/1');
|
||||
// => [Function, Function]
|
||||
</code></pre>
|
||||
|
||||
<p> This second call returns only the callback for <em>/user/:id/:op?</em>.</p>
|
||||
|
||||
<pre><code> app.match.get('/user/23/edit');
|
||||
// => [Function]
|
||||
</code></pre>
|
||||
|
||||
<p> We can also use <em>all()</em> to disregard the http method:</p>
|
||||
|
||||
<pre><code> app.match.all('/user/20');
|
||||
// => [Function, Function, Function]
|
||||
</code></pre>
|
||||
|
||||
<p> Each function matched has the following properties:</p>
|
||||
|
||||
<pre><code> var fn = app.match.get('/user/23/edit')[0];
|
||||
|
||||
fn.keys
|
||||
// => ['id', 'op']
|
||||
|
||||
fn.params
|
||||
// => { id: '23', op: 'edit' }
|
||||
|
||||
fn.method
|
||||
// => 'GET'
|
||||
</code></pre>
|
||||
|
||||
<h3 id="app.mounted()">app.mounted(fn)</h3>
|
||||
|
||||
<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>
|
||||
|
||||
118
docs/guide.md
118
docs/guide.md
@@ -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
|
||||
|
||||
@@ -165,7 +166,7 @@ For example we can __POST__ some json, and echo the json back using the _bodyPar
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
Typically we may use a "dumb" placeholder such as "/user/:id" which has no restrictions, however say for example we are limiting a user id to digits, we may use _'/user/:id(\\d+)'_ which will _not_ match unless the placeholder value contains only digits.
|
||||
Typically we may use a "dumb" placeholder such as "/user/:id" which has no restrictions, however say for example we are limiting a user id to digits, we may use _'/user/:id(\\\\d+)'_ which will _not_ match unless the placeholder value contains only digits.
|
||||
|
||||
### Passing Route Control
|
||||
|
||||
@@ -816,7 +817,7 @@ Options may also be passed to the internal _fs.createReadStream()_ call, for exa
|
||||
// handle
|
||||
});
|
||||
|
||||
### res.download(file[, filename[, callback]])
|
||||
### res.download(file[, filename[, callback[, callback2]]])
|
||||
|
||||
Transfer the given _file_ as an attachment with optional alternative _filename_.
|
||||
|
||||
@@ -828,12 +829,20 @@ This is equivalent to:
|
||||
res.attachment(file);
|
||||
res.sendfile(file);
|
||||
|
||||
An optional callback may be supplied as either the second or third argument, which is passed to _res.sendfile()_:
|
||||
An optional callback may be supplied as either the second or third argument, which is passed to _res.sendfile()_. Within this callback you may still respond, as the header has not been sent.
|
||||
|
||||
res.download(path, 'expenses.doc', function(err){
|
||||
// handle
|
||||
});
|
||||
|
||||
An optional second callback, _callback2_ may be given to allow you to act on connection related errors, however you should not attempt to respond.
|
||||
|
||||
res.download(path, function(err){
|
||||
// error or finished
|
||||
}, function(err){
|
||||
// connection related error
|
||||
});
|
||||
|
||||
### res.send(body|status[, headers|status[, status]])
|
||||
|
||||
The _res.send()_ method is a high level response utility allowing you to pass
|
||||
@@ -878,7 +887,7 @@ The _maxAge_ property may be used to set _expires_ relative to _Date.now()_ in m
|
||||
|
||||
res.cookie('rememberme', 'yes', { maxAge: 900000 });
|
||||
|
||||
To parse incoming _Cookie_ headers, use the _cookieDecoder_ middleware, which provides the _req.cookies_ object:
|
||||
To parse incoming _Cookie_ headers, use the _cookieParser_ middleware, which provides the _req.cookies_ object:
|
||||
|
||||
app.use(express.cookieParser());
|
||||
|
||||
@@ -886,7 +895,7 @@ To parse incoming _Cookie_ headers, use the _cookieDecoder_ middleware, which pr
|
||||
// use req.cookies.rememberme
|
||||
});
|
||||
|
||||
### res.clearCookie(name)
|
||||
### res.clearCookie(name[, options])
|
||||
|
||||
Clear cookie _name_ by setting "expires" far in the past.
|
||||
|
||||
@@ -1145,9 +1154,7 @@ Express also provides a few locals by default:
|
||||
|
||||
- `settings` the app's settings object
|
||||
- `filename` the view's filename
|
||||
- `request` the request object
|
||||
- `response` the response object
|
||||
- `app` the application itself
|
||||
- `layout(path)` specify the layout from within a view
|
||||
|
||||
This method is aliased as _app.locals()_.
|
||||
|
||||
@@ -1168,6 +1175,101 @@ All views would now have _session_ available so that session data can be accesse
|
||||
|
||||
<%= session.name %>
|
||||
|
||||
### app.lookup
|
||||
|
||||
The _app.lookup_ http methods returns an array of callback functions
|
||||
associated with the given _path_.
|
||||
|
||||
Suppose we define the following routes:
|
||||
|
||||
app.get('/user/:id', function(){});
|
||||
app.put('/user/:id', function(){});
|
||||
app.get('/user/:id/:op?', function(){});
|
||||
|
||||
We can utilize this lookup functionality to check which routes
|
||||
have been defined, which can be extremely useful for higher level
|
||||
frameworks built on Express.
|
||||
|
||||
app.lookup.get('/user/:id');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.get('/user/:id/:op?');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.put('/user/:id');
|
||||
// => [Function]
|
||||
|
||||
app.lookup.all('/user/:id');
|
||||
// => [Function, Function]
|
||||
|
||||
app.lookup.all('/hey');
|
||||
// => []
|
||||
|
||||
To alias _app.lookup.VERB()_, we can simply invoke _app.VERB()_
|
||||
without a callback, as a shortcut, for example the following are
|
||||
equivalent:
|
||||
|
||||
app.lookup.get('/user');
|
||||
app.get('/user');
|
||||
|
||||
Each function returned has the following properties:
|
||||
|
||||
var fn = app.get('/user/:id/:op?')[0];
|
||||
|
||||
fn.regexp
|
||||
// => /^\/user\/(?:([^\/]+?))(?:\/([^\/]+?))?\/?$/i
|
||||
|
||||
fn.keys
|
||||
// => ['id', 'op']
|
||||
|
||||
fn.path
|
||||
// => '/user/:id/:op?'
|
||||
|
||||
fn.method
|
||||
// => 'GET'
|
||||
|
||||
### app.match
|
||||
|
||||
The _app.match_ http methods return an array of callback functions
|
||||
which match the given _url_, which may include a query string etc. This
|
||||
is useful when you want reflect on which routes have the opportunity to
|
||||
respond.
|
||||
|
||||
Suppose we define the following routes:
|
||||
|
||||
app.get('/user/:id', function(){});
|
||||
app.put('/user/:id', function(){});
|
||||
app.get('/user/:id/:op?', function(){});
|
||||
|
||||
Our match against __GET__ will return two functions, since the _:op_
|
||||
in our second route is optional.
|
||||
|
||||
app.match.get('/user/1');
|
||||
// => [Function, Function]
|
||||
|
||||
This second call returns only the callback for _/user/:id/:op?_.
|
||||
|
||||
app.match.get('/user/23/edit');
|
||||
// => [Function]
|
||||
|
||||
We can also use _all()_ to disregard the http method:
|
||||
|
||||
app.match.all('/user/20');
|
||||
// => [Function, Function, Function]
|
||||
|
||||
Each function matched has the following properties:
|
||||
|
||||
var fn = app.match.get('/user/23/edit')[0];
|
||||
|
||||
fn.keys
|
||||
// => ['id', 'op']
|
||||
|
||||
fn.params
|
||||
// => { id: '23', op: 'edit' }
|
||||
|
||||
fn.method
|
||||
// => 'GET'
|
||||
|
||||
### app.mounted(fn)
|
||||
|
||||
Assign a callback _fn_ which is called when this _Server_ is passed to _Server#use()_.
|
||||
|
||||
@@ -237,7 +237,7 @@ app.listen(3000);
|
||||
<ul>
|
||||
<li><a href="http://github.com/visionmedia/express-resource">express-resource</a> provides resourceful routing</li>
|
||||
<li><a href="http://github.com/visionmedia/express-messages">express-messages</a> flash message notification rendering</li>
|
||||
<li><a href="http://github.com/visionmedia/express-configure">express-configure</a> async configuration support (load settings from redis etc)</li>
|
||||
<li><a href="http://github.com/visionmedia/express-configuration">express-configure</a> async configuration support (load settings from redis etc)</li>
|
||||
<li><a href="http://github.com/visionmedia/express-namespace">express-namespace</a> namespaced routing support</li>
|
||||
</ul>
|
||||
|
||||
|
||||
35
examples/layout-control/app.js
Normal file
35
examples/layout-control/app.js
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
// Expose modules in ./support for demo purposes
|
||||
require.paths.unshift(__dirname + '/../../support');
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../lib/express');
|
||||
|
||||
var app = express.createServer();
|
||||
|
||||
app.set('views', __dirname + '/views');
|
||||
|
||||
// set default layout, usually "layout"
|
||||
app.set('view options', { layout: 'layouts/default' });
|
||||
|
||||
// Set our default template engine to "jade"
|
||||
// which prevents the need for extensions
|
||||
// (although you can still mix and match)
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.render('pages/default');
|
||||
});
|
||||
|
||||
app.get('/alternate', function(req, res){
|
||||
// note that we do not explicitly
|
||||
// state the layout here, the view does,
|
||||
// although we could do it here as well.
|
||||
res.render('pages/alternate');
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
console.log('Express app started on port 3000');
|
||||
6
examples/layout-control/views/layouts/alternate.ejs
Normal file
6
examples/layout-control/views/layouts/alternate.ejs
Normal file
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>Alternate Layout</h1>
|
||||
<%- body %>
|
||||
</body>
|
||||
</html>
|
||||
6
examples/layout-control/views/layouts/default.ejs
Normal file
6
examples/layout-control/views/layouts/default.ejs
Normal file
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>Default Layout</h1>
|
||||
<%- body %>
|
||||
</body>
|
||||
</html>
|
||||
2
examples/layout-control/views/pages/alternate.ejs
Normal file
2
examples/layout-control/views/pages/alternate.ejs
Normal file
@@ -0,0 +1,2 @@
|
||||
<% layout('layouts/alternate') %>
|
||||
<h1>Page</h1>
|
||||
1
examples/layout-control/views/pages/default.ejs
Normal file
1
examples/layout-control/views/pages/default.ejs
Normal file
@@ -0,0 +1 @@
|
||||
<h1>Page</h1>
|
||||
@@ -14,7 +14,7 @@ exports.boot = function(app){
|
||||
// App settings and middleware
|
||||
|
||||
function bootApplication(app) {
|
||||
app.use(express.logger({ format: ':method :url :status' }));
|
||||
app.use(express.logger(':method :url :status'));
|
||||
app.use(express.bodyParser());
|
||||
app.use(express.methodOverride());
|
||||
app.use(express.cookieParser());
|
||||
@@ -45,6 +45,7 @@ function bootApplication(app) {
|
||||
},
|
||||
|
||||
hasMessages: function(req){
|
||||
if (!req.session) return false;
|
||||
return Object.keys(req.session.flash || {}).length;
|
||||
},
|
||||
|
||||
|
||||
@@ -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.1.0';
|
||||
exports.version = '2.3.2';
|
||||
|
||||
/**
|
||||
* Shortcut for `new Server(...)`.
|
||||
@@ -51,6 +52,7 @@ exports.createServer = function(options){
|
||||
|
||||
exports.HTTPServer = HTTPServer;
|
||||
exports.HTTPSServer = HTTPSServer;
|
||||
exports.Route = Route;
|
||||
|
||||
/**
|
||||
* View extensions.
|
||||
@@ -69,3 +71,8 @@ require('./response');
|
||||
*/
|
||||
|
||||
require('./request');
|
||||
|
||||
// Error handler title
|
||||
|
||||
exports.errorHandler.title = 'Express';
|
||||
|
||||
|
||||
135
lib/http.js
135
lib/http.js
@@ -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,19 +54,17 @@ Server.prototype.__proto__ = connect.HTTPServer.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Server.prototype.init = function(middleware){
|
||||
app.init = function(middleware){
|
||||
var self = this;
|
||||
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
|
||||
@@ -89,12 +99,6 @@ Server.prototype.init = function(middleware){
|
||||
, app: this
|
||||
});
|
||||
|
||||
// default dynamic locals
|
||||
this.dynamicHelpers({
|
||||
request: function(req, res){ return req; }
|
||||
, response: function(req, res){ return res; }
|
||||
});
|
||||
|
||||
// default development configuration
|
||||
this.configure('development', function(){
|
||||
this.enable('hints');
|
||||
@@ -102,19 +106,52 @@ 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"
|
||||
// so that they disregard definition position.
|
||||
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
|
||||
? null
|
||||
: 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
|
||||
: method);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* When using the vhost() middleware register error handlers.
|
||||
*/
|
||||
|
||||
Server.prototype.onvhost = function(){
|
||||
app.onvhost = function(){
|
||||
this.registerErrorHandlers();
|
||||
};
|
||||
|
||||
@@ -125,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);
|
||||
@@ -144,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) {
|
||||
@@ -202,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;
|
||||
};
|
||||
@@ -214,7 +251,7 @@ Server.prototype.mounted = function(fn){
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.register = function(){
|
||||
app.register = function(){
|
||||
view.register.apply(this, arguments);
|
||||
return this;
|
||||
};
|
||||
@@ -228,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;
|
||||
};
|
||||
|
||||
@@ -243,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;
|
||||
};
|
||||
@@ -291,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);
|
||||
@@ -312,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;
|
||||
};
|
||||
@@ -326,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;
|
||||
@@ -342,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];
|
||||
@@ -363,7 +400,7 @@ Server.prototype.set = function(setting, val){
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.enabled = function(setting){
|
||||
app.enabled = function(setting){
|
||||
return !!this.set(setting);
|
||||
};
|
||||
|
||||
@@ -375,7 +412,7 @@ Server.prototype.enabled = function(setting){
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.disabled = function(setting){
|
||||
app.disabled = function(setting){
|
||||
return !this.set(setting);
|
||||
};
|
||||
|
||||
@@ -387,7 +424,7 @@ Server.prototype.disabled = function(setting){
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.enable = function(setting){
|
||||
app.enable = function(setting){
|
||||
return this.set(setting, true);
|
||||
};
|
||||
|
||||
@@ -399,7 +436,7 @@ Server.prototype.enable = function(setting){
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.disable = function(setting){
|
||||
app.disable = function(setting){
|
||||
return this.set(setting, false);
|
||||
};
|
||||
|
||||
@@ -412,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;
|
||||
};
|
||||
@@ -426,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';
|
||||
}
|
||||
@@ -439,31 +476,21 @@ 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;
|
||||
|
||||
// Ensure router is mounted
|
||||
if (!this.__usedRouter) {
|
||||
this.use(this.router);
|
||||
// Lookup
|
||||
if (1 == arguments.length) {
|
||||
return this.router.lookup(path, 'all' == method
|
||||
? null
|
||||
: method);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
// Ensure router is mounted
|
||||
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;
|
||||
@@ -473,4 +500,4 @@ methods.forEach(generateMethod);
|
||||
|
||||
// Alias delete as "del"
|
||||
|
||||
Server.prototype.del = Server.prototype.delete;
|
||||
app.del = app.delete;
|
||||
|
||||
18
lib/https.js
18
lib/https.js
@@ -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];
|
||||
});
|
||||
|
||||
@@ -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++]);
|
||||
|
||||
@@ -51,7 +51,7 @@ res.send = function(body, headers, status){
|
||||
status = status || this.statusCode;
|
||||
|
||||
// allow 0 args as 204
|
||||
if (!arguments.length) body = status = 204;
|
||||
if (!arguments.length || undefined === body) body = status = 204;
|
||||
|
||||
// determine content type
|
||||
switch (typeof body) {
|
||||
@@ -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 + ');';
|
||||
}
|
||||
@@ -130,6 +132,7 @@ res.send = function(body, headers, status){
|
||||
*/
|
||||
|
||||
res.sendfile = function(path, options, fn){
|
||||
var next = this.req.next;
|
||||
options = options || {};
|
||||
|
||||
// support function as second arg
|
||||
@@ -140,7 +143,7 @@ res.sendfile = function(path, options, fn){
|
||||
|
||||
options.path = path;
|
||||
options.callback = fn;
|
||||
send(this.req, this, this.req.next, options);
|
||||
send(this.req, this, next, options);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -175,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');
|
||||
@@ -183,32 +187,41 @@ res.attachment = function(filename){
|
||||
|
||||
/**
|
||||
* Transfer the file at the given `path`, with optional
|
||||
* `filename` as an attachment and optional callback `fn(err)`.
|
||||
* `filename` as an attachment and optional callback `fn(err)`,
|
||||
* and optional `fn2(err)` which is invoked when an error has
|
||||
* occurred after headers have been sent.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {String|Function} filename or fn
|
||||
* @param {Function} fn
|
||||
* @return {Type}
|
||||
* @param {Function} fn2
|
||||
* @api public
|
||||
*/
|
||||
|
||||
res.download = function(path, filename, fn){
|
||||
res.download = function(path, filename, fn, fn2){
|
||||
var self = this;
|
||||
|
||||
// support callback as second arg
|
||||
if ('function' == typeof filename) {
|
||||
fn2 = fn;
|
||||
fn = filename;
|
||||
filename = null;
|
||||
}
|
||||
|
||||
// transfer the file
|
||||
this.attachment(filename || path).sendfile(path, function(err){
|
||||
if (err) self.removeHeader('Content-Disposition');
|
||||
if (fn) return fn(err);
|
||||
var sentHeader = self._header;
|
||||
if (err) {
|
||||
self.req.next('ENOENT' == err.code
|
||||
? null
|
||||
: err);
|
||||
if (!sentHeader) self.removeHeader('Content-Disposition');
|
||||
if (sentHeader) {
|
||||
fn2 && fn2(err);
|
||||
} else if (fn) {
|
||||
fn(err);
|
||||
} else {
|
||||
self.req.next(err);
|
||||
}
|
||||
} else if (fn) {
|
||||
fn();
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -235,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);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -394,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
329
lib/router/index.js
Normal 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
31
lib/router/methods.js
Normal 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
64
lib/router/route.js
Normal 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');
|
||||
}
|
||||
81
lib/utils.js
81
lib/utils.js
@@ -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, '&')
|
||||
.replace(/"/g, '"')
|
||||
|
||||
157
lib/view.js
157
lib/view.js
@@ -21,14 +21,6 @@ var path = require('path')
|
||||
, http = require('http')
|
||||
, res = http.ServerResponse.prototype;
|
||||
|
||||
/**
|
||||
* Memory cache.
|
||||
*
|
||||
* @type Object
|
||||
*/
|
||||
|
||||
var cache = {};
|
||||
|
||||
/**
|
||||
* Expose constructors.
|
||||
*/
|
||||
@@ -113,16 +105,39 @@ function renderPartial(res, view, options, parentLocals, parent){
|
||||
// Collection support
|
||||
if (collection) {
|
||||
var len = collection.length
|
||||
, buf = '';
|
||||
, buf = ''
|
||||
, keys
|
||||
, key
|
||||
, val;
|
||||
|
||||
options.collectionLength = len;
|
||||
for (var i = 0; i < len; ++i) {
|
||||
var val = collection[i];
|
||||
options.firstInCollection = i === 0;
|
||||
options.indexInCollection = i;
|
||||
options.lastInCollection = i === len - 1;
|
||||
object = val;
|
||||
buf += render();
|
||||
|
||||
if ('number' == typeof len || Array.isArray(collection)) {
|
||||
for (var i = 0; i < len; ++i) {
|
||||
val = collection[i];
|
||||
options.firstInCollection = i == 0;
|
||||
options.indexInCollection = i;
|
||||
options.lastInCollection = i == len - 1;
|
||||
object = val;
|
||||
buf += render();
|
||||
}
|
||||
} else {
|
||||
keys = Object.keys(collection);
|
||||
len = keys.length;
|
||||
options.collectionLength = len;
|
||||
options.collectionKeys = keys;
|
||||
for (var i = 0; i < len; ++i) {
|
||||
key = keys[i];
|
||||
val = collection[key];
|
||||
options.keyInCollection = key;
|
||||
options.firstInCollection = i == 0;
|
||||
options.indexInCollection = i;
|
||||
options.lastInCollection = i == len - 1;
|
||||
object = val;
|
||||
buf += render();
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
} else {
|
||||
return render();
|
||||
@@ -179,8 +194,9 @@ res.partial = function(view, options, fn){
|
||||
if (fn) {
|
||||
fn(err);
|
||||
} else {
|
||||
throw err;
|
||||
this.req.next(err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// callback or transfer
|
||||
@@ -220,7 +236,8 @@ res.render = function(view, opts, fn, parent, sub){
|
||||
// callback given
|
||||
if (fn) {
|
||||
fn(err);
|
||||
// unwind to root call
|
||||
// unwind to root call to prevent
|
||||
// several next(err) calls
|
||||
} else if (sub) {
|
||||
throw err;
|
||||
// root template, next(err)
|
||||
@@ -236,10 +253,14 @@ 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')
|
||||
, cacheTemplates = 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);
|
||||
@@ -256,10 +277,10 @@ res._render = function(view, opts, fn, parent, sub){
|
||||
// status support
|
||||
if (options.status) this.statusCode = options.status;
|
||||
|
||||
// Defaults
|
||||
var self = this
|
||||
, root = app.set('views') || process.cwd() + '/views'
|
||||
, partial = options.renderPartial
|
||||
// capture attempts
|
||||
options.attempts = [];
|
||||
|
||||
var partial = options.renderPartial
|
||||
, layout = options.layout;
|
||||
|
||||
// Layout support
|
||||
@@ -282,31 +303,6 @@ res._render = function(view, opts, fn, parent, sub){
|
||||
// charset option
|
||||
if (options.charset) this.charset = options.charset;
|
||||
|
||||
// Populate view
|
||||
var orig = view = new View(view, options);
|
||||
|
||||
// Try _ prefix ex: ./views/_<name>.jade
|
||||
if (!view.exists) view = new View(orig.prefixPath, options);
|
||||
|
||||
// Try index ex: ./views/user/index.jade
|
||||
if (!view.exists) view = new View(orig.indexPath, options);
|
||||
|
||||
// Try ../<name>/index ex: ../user/index.jade
|
||||
// when calling partial('user') within the same dir
|
||||
if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);
|
||||
|
||||
// Try root ex: <root>/user.jade
|
||||
if (!view.exists) view = new View(orig.rootPath, options);
|
||||
|
||||
// Try root _ prefix ex: <root>/_user.jade
|
||||
if (!view.exists && partial) view = new View(view.prefixPath, options);
|
||||
|
||||
// Does not exist
|
||||
if (!view.exists) {
|
||||
if (app.enabled('hints')) hintAtViewPaths(orig, options);
|
||||
throw new Error('failed to locate view "' + orig.view + '"');
|
||||
}
|
||||
|
||||
// Dynamic helper support
|
||||
if (false !== options.dynamicHelpers) {
|
||||
// cache
|
||||
@@ -332,15 +328,54 @@ res._render = function(view, opts, fn, parent, sub){
|
||||
return renderPartial(self, path, opts, options, view);
|
||||
};
|
||||
|
||||
// Provide filename to engine
|
||||
options.filename = view.path;
|
||||
// cached view
|
||||
if (app.cache[cid]) {
|
||||
view = app.cache[cid];
|
||||
options.filename = view.path;
|
||||
// resolve view
|
||||
} else {
|
||||
var orig = view = new View(view, options);
|
||||
|
||||
// Attempt render
|
||||
var engine = view.templateEngine
|
||||
, template = cacheTemplates
|
||||
? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))
|
||||
: engine.compile(view.contents, options)
|
||||
, str = template.call(options.scope, options);
|
||||
// Try _ prefix ex: ./views/_<name>.jade
|
||||
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);
|
||||
|
||||
// Try ../<name>/index ex: ../user/index.jade
|
||||
// when calling partial('user') within the same dir
|
||||
if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);
|
||||
|
||||
// Try root ex: <root>/user.jade
|
||||
if (!view.exists) view = new View(orig.rootPath, options);
|
||||
|
||||
// Try root _ prefix ex: <root>/_user.jade
|
||||
if (!view.exists && partial) view = new View(view.prefixPath, options);
|
||||
|
||||
// Does not exist
|
||||
if (!view.exists) {
|
||||
if (app.enabled('hints')) hintAtViewPaths(orig, options);
|
||||
var err = new Error('failed to locate view "' + orig.view + '"');
|
||||
err.view = orig;
|
||||
throw err;
|
||||
}
|
||||
|
||||
options.filename = view.path;
|
||||
var engine = view.templateEngine;
|
||||
view.fn = engine.compile(view.contents, options)
|
||||
if (cacheViews) app.cache[cid] = view;
|
||||
}
|
||||
|
||||
// layout helper
|
||||
options.layout = function(path){
|
||||
layout = path;
|
||||
};
|
||||
|
||||
// render
|
||||
var str = view.fn.call(options.scope, options);
|
||||
|
||||
// layout expected
|
||||
if (layout) {
|
||||
@@ -371,10 +406,8 @@ res._render = function(view, opts, fn, parent, sub){
|
||||
function hintAtViewPaths(view, options) {
|
||||
console.error();
|
||||
console.error('failed to locate view "' + view.view + '", tried:');
|
||||
console.error(' - ' + new View(view.path, options).path);
|
||||
console.error(' - ' + new View(view.prefixPath, options).path);
|
||||
console.error(' - ' + new View(view.indexPath, options).path);
|
||||
if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);
|
||||
if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);
|
||||
options.attempts.forEach(function(path){
|
||||
console.error(' - %s', path);
|
||||
});
|
||||
console.error();
|
||||
}
|
||||
@@ -9,25 +9,25 @@
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Memory cache.
|
||||
* Expose `View`.
|
||||
*/
|
||||
|
||||
exports = module.exports = View;
|
||||
|
||||
/**
|
||||
* Require cache.
|
||||
*/
|
||||
|
||||
var cache = {};
|
||||
|
||||
/**
|
||||
* Existance cache.
|
||||
*/
|
||||
|
||||
var exists = {};
|
||||
|
||||
/**
|
||||
* Initialize a new `View` with the given `view` path and `options`.
|
||||
*
|
||||
@@ -36,9 +36,8 @@ var exists = {};
|
||||
* @api private
|
||||
*/
|
||||
|
||||
var View = exports = module.exports = function View(view, options) {
|
||||
function View(view, options) {
|
||||
options = options || {};
|
||||
// TODO: more caching
|
||||
this.view = view;
|
||||
this.root = options.root;
|
||||
this.relative = false !== options.relative;
|
||||
@@ -50,6 +49,7 @@ var View = exports = module.exports = function View(view, options) {
|
||||
this.name = this.basename.replace(this.extension, '');
|
||||
this.path = this.resolvePath();
|
||||
this.dirname = dirname(this.path);
|
||||
options.attempts.push(this.path);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -60,16 +60,11 @@ var View = exports = module.exports = function View(view, options) {
|
||||
*/
|
||||
|
||||
View.prototype.__defineGetter__('exists', function(){
|
||||
var path = this.path;
|
||||
if (null != exists[path]) {
|
||||
return exists[path];
|
||||
} else {
|
||||
try {
|
||||
stat(path);
|
||||
return exists[path] = true;
|
||||
} catch (err) {
|
||||
return exists[path] = false;
|
||||
}
|
||||
try {
|
||||
stat(this.path);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "express",
|
||||
"description": "Sinatra inspired web development framework",
|
||||
"version": "2.1.0",
|
||||
"version": "2.3.2",
|
||||
"author": "TJ Holowaychuk <tj@vision-media.ca>",
|
||||
"contributors": [
|
||||
{ "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" },
|
||||
@@ -10,11 +10,12 @@
|
||||
{ "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
|
||||
],
|
||||
"dependencies": {
|
||||
"connect": ">= 1.1.1 < 2.0.0",
|
||||
"connect": ">= 1.4.0 < 2.0.0",
|
||||
"mime": ">= 0.0.1",
|
||||
"qs": ">= 0.0.6"
|
||||
},
|
||||
"keywords": ["framework", "sinatra", "web", "rest", "restful"],
|
||||
"repository": "git://github.com/visionmedia/express",
|
||||
"main": "index",
|
||||
"bin": { "express": "./bin/express" },
|
||||
"engines": { "node": ">= 0.4.1 < 0.5.0" }
|
||||
|
||||
Submodule support/connect updated: 2b3cea7744...af32d828a3
Submodule support/connect-form updated: ccefcd28db...e861cc85d6
Submodule support/ejs updated: 673e6f23cb...499c4815a5
Submodule support/expresso updated: 2c8759f147...855e0dea07
Submodule support/formidable updated: 63347d825b...5d98e9c75c
Submodule support/haml updated: 4122210f38...55bb6fdc79
Submodule support/jade updated: 1e23782b6f...bdeb8b9fb4
@@ -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,133 +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' }});
|
||||
}
|
||||
};
|
||||
|
||||
1
test/fixtures/_foobar.jade
vendored
Normal file
1
test/fixtures/_foobar.jade
vendored
Normal file
@@ -0,0 +1 @@
|
||||
p two
|
||||
1
test/fixtures/error.jade
vendored
Normal file
1
test/fixtures/error.jade
vendored
Normal file
@@ -0,0 +1 @@
|
||||
= user.name
|
||||
1
test/fixtures/foobar.jade
vendored
Normal file
1
test/fixtures/foobar.jade
vendored
Normal file
@@ -0,0 +1 @@
|
||||
p one
|
||||
2
test/fixtures/layout-switch.jade
vendored
Normal file
2
test/fixtures/layout-switch.jade
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
- layout('layouts/alternate')
|
||||
h1 My Page
|
||||
1
test/fixtures/layouts/alternate.jade
vendored
Normal file
1
test/fixtures/layouts/alternate.jade
vendored
Normal file
@@ -0,0 +1 @@
|
||||
#alternate!= body
|
||||
1
test/fixtures/object-item.jade
vendored
Normal file
1
test/fixtures/object-item.jade
vendored
Normal file
@@ -0,0 +1 @@
|
||||
li #{keyInCollection}: #{item}
|
||||
@@ -50,6 +50,10 @@ module.exports = {
|
||||
res.send();
|
||||
});
|
||||
|
||||
app.get('/undefined', function(req, res, next){
|
||||
res.send(undefined);
|
||||
});
|
||||
|
||||
app.get('/bool', function(req, res, next){
|
||||
res.send(true);
|
||||
});
|
||||
@@ -57,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' },
|
||||
@@ -72,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'
|
||||
}});
|
||||
|
||||
@@ -81,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'
|
||||
}});
|
||||
|
||||
@@ -89,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'
|
||||
}});
|
||||
|
||||
@@ -98,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'
|
||||
}});
|
||||
|
||||
@@ -107,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'
|
||||
}});
|
||||
|
||||
@@ -148,6 +152,13 @@ module.exports = {
|
||||
assert.equal(undefined, res.headers['content-type']);
|
||||
assert.equal(undefined, res.headers['content-length']);
|
||||
});
|
||||
|
||||
assert.response(app,
|
||||
{ url: '/undefined' },
|
||||
{ status: 204 }, function(res){
|
||||
assert.equal(undefined, res.headers['content-type']);
|
||||
assert.equal(undefined, res.headers['content-length']);
|
||||
});
|
||||
},
|
||||
|
||||
'test #contentType()': function(){
|
||||
@@ -196,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(){
|
||||
@@ -557,7 +570,7 @@ module.exports = {
|
||||
var app = express.createServer();
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.clearCookie('rememberme');
|
||||
res.clearCookie('rememberme', { path: '/foo' });
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
@@ -565,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
245
test/router.test.js
Normal 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);
|
||||
}
|
||||
};
|
||||
@@ -287,6 +287,19 @@ module.exports = {
|
||||
{ body: '<cool><p>Welcome</p></cool>' });
|
||||
},
|
||||
|
||||
'test #render() view layout control': function(){
|
||||
var app = create();
|
||||
app.set('view engine', 'jade');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.render('layout-switch');
|
||||
});
|
||||
|
||||
assert.response(app,
|
||||
{ url: '/' },
|
||||
{ body: '<div id="alternate"><h1>My Page</h1></div>' });
|
||||
},
|
||||
|
||||
'test #render() "view engine" with periods in dirname': function(){
|
||||
var app = create();
|
||||
app.set('view engine', 'jade');
|
||||
@@ -358,6 +371,19 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
'test #partial() collection object': function(){
|
||||
var app = create();
|
||||
|
||||
app.get('/', function(req, res){
|
||||
var items = { 2: 'foo', bar: 'bar' };
|
||||
res.partial('object-item.jade', { as: 'item', collection: items });
|
||||
});
|
||||
|
||||
assert.response(app,
|
||||
{ url: '/' },
|
||||
{ body: '<li>2: foo</li><li>bar: bar</li>' });
|
||||
},
|
||||
|
||||
'test #partial()': function(){
|
||||
var app = create();
|
||||
|
||||
@@ -619,6 +645,26 @@ module.exports = {
|
||||
assert.response(app,
|
||||
{ url: '/root/underscore' },
|
||||
{ body: '<p>Testing</p>' });
|
||||
|
||||
// error in template
|
||||
|
||||
app.get('/error', function(req, res){
|
||||
process.nextTick(function(){
|
||||
res.partial('error');
|
||||
});
|
||||
});
|
||||
|
||||
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(){
|
||||
|
||||
Reference in New Issue
Block a user