mirror of
https://github.com/expressjs/express.git
synced 2026-02-26 18:45:05 +00:00
Compare commits
112 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1c46f51e5 | ||
|
|
297fb4e0b0 | ||
|
|
30b7aa8a17 | ||
|
|
c1d16e0016 | ||
|
|
929ffb8d77 | ||
|
|
197a2e3b54 | ||
|
|
6cf6c8b918 | ||
|
|
752b5f705e | ||
|
|
e7fa579637 | ||
|
|
97781d4112 | ||
|
|
3ddd8e66a7 | ||
|
|
8a1e865e37 | ||
|
|
e850cb3ea3 | ||
|
|
13d3efe8df | ||
|
|
d6ecf785a2 | ||
|
|
a38bdf6758 | ||
|
|
5aa9670120 | ||
|
|
8ad8cb93cc | ||
|
|
610e172fcf | ||
|
|
6942070a21 | ||
|
|
e283200511 | ||
|
|
54a192a5c5 | ||
|
|
c3bd65eda2 | ||
|
|
3de81e0147 | ||
|
|
8fe1e2a5b4 | ||
|
|
909dbb81d5 | ||
|
|
26802a689c | ||
|
|
37239fb67f | ||
|
|
018dc40b32 | ||
|
|
52440955e6 | ||
|
|
c2f3d6ce2b | ||
|
|
be858f5d07 | ||
|
|
1f14734f91 | ||
|
|
6d1d694dbb | ||
|
|
ba5c48aa86 | ||
|
|
320d7807a9 | ||
|
|
6650a312b7 | ||
|
|
832c3b3744 | ||
|
|
76691bfd6b | ||
|
|
29fe5ea785 | ||
|
|
52a820113f | ||
|
|
aec3428489 | ||
|
|
a10f695b6f | ||
|
|
a3c9eacaf1 | ||
|
|
19d685b152 | ||
|
|
8ab44081d4 | ||
|
|
0431d22822 | ||
|
|
138d74aefa | ||
|
|
28562b2cf8 | ||
|
|
e0afda444f | ||
|
|
5a4cac58af | ||
|
|
545dca6c4d | ||
|
|
e59a882389 | ||
|
|
ccd9828535 | ||
|
|
41f0d32355 | ||
|
|
2f19b4fefc | ||
|
|
cd31cecfd1 | ||
|
|
2fe46b3905 | ||
|
|
24974f1f8f | ||
|
|
fd73bd006e | ||
|
|
7388c2c223 | ||
|
|
e2210b0b92 | ||
|
|
30919be2a0 | ||
|
|
10b21b41f7 | ||
|
|
28b8a3b5f7 | ||
|
|
8b2f1bba95 | ||
|
|
c805d80a9b | ||
|
|
d876778d22 | ||
|
|
412e571600 | ||
|
|
3296ed9cb3 | ||
|
|
91835e6816 | ||
|
|
f976625281 | ||
|
|
028d9d8a0c | ||
|
|
8559c0e2a4 | ||
|
|
06ead58240 | ||
|
|
28ca1b5221 | ||
|
|
6d872e6693 | ||
|
|
0b09c8981f | ||
|
|
a1d5676ecb | ||
|
|
f862ad29f5 | ||
|
|
15496da8fd | ||
|
|
802fb1632c | ||
|
|
69453ff889 | ||
|
|
28752cc3c0 | ||
|
|
9f06d9b03f | ||
|
|
3fb7c4e1db | ||
|
|
5fa685b602 | ||
|
|
eb1bbb92c0 | ||
|
|
d0e49f1a8a | ||
|
|
ea2664a4b8 | ||
|
|
da6524bd06 | ||
|
|
a231406931 | ||
|
|
6d39ed8ef7 | ||
|
|
f2563f4dde | ||
|
|
3df265b36a | ||
|
|
e382e6adc7 | ||
|
|
91c71d6c2e | ||
|
|
0d40c65b7f | ||
|
|
58f2057ba7 | ||
|
|
37179109db | ||
|
|
579857cfaa | ||
|
|
0b4e2df480 | ||
|
|
49cc1a70b1 | ||
|
|
f8a33d137a | ||
|
|
2db135dfc7 | ||
|
|
99bc628ad1 | ||
|
|
5ba6c301d7 | ||
|
|
88273a59f8 | ||
|
|
2e53cb72ec | ||
|
|
3b1597d79e | ||
|
|
ee9d50c128 | ||
|
|
57e48c4767 |
93
History.md
93
History.md
@@ -1,4 +1,97 @@
|
||||
|
||||
3.3.8 / 2013-09-02
|
||||
==================
|
||||
|
||||
* update connect
|
||||
|
||||
3.3.7 / 2013-08-28
|
||||
==================
|
||||
|
||||
* update connect
|
||||
|
||||
3.3.6 / 2013-08-27
|
||||
==================
|
||||
|
||||
* Revert "remove charset from json responses. Closes #1631" (causes issues in some clients)
|
||||
* add: req.accepts take an argument list
|
||||
|
||||
3.3.4 / 2013-07-08
|
||||
==================
|
||||
|
||||
* update send and connect
|
||||
|
||||
3.3.3 / 2013-07-04
|
||||
==================
|
||||
|
||||
* update connect
|
||||
|
||||
3.3.2 / 2013-07-03
|
||||
==================
|
||||
|
||||
* update connect
|
||||
* update send
|
||||
* remove .version export
|
||||
|
||||
3.3.1 / 2013-06-27
|
||||
==================
|
||||
|
||||
* update connect
|
||||
|
||||
3.3.0 / 2013-06-26
|
||||
==================
|
||||
|
||||
* update connect
|
||||
* add support for multiple X-Forwarded-Proto values. Closes #1646
|
||||
* change: remove charset from json responses. Closes #1631
|
||||
* change: return actual booleans from req.accept* functions
|
||||
* fix jsonp callback array throw
|
||||
|
||||
3.2.6 / 2013-06-02
|
||||
==================
|
||||
|
||||
* update connect
|
||||
|
||||
3.2.5 / 2013-05-21
|
||||
==================
|
||||
|
||||
* update connect
|
||||
* update node-cookie
|
||||
* add: throw a meaningful error when there is no default engine
|
||||
* change generation of ETags with res.send() to GET requests only. Closes #1619
|
||||
|
||||
3.2.4 / 2013-05-09
|
||||
==================
|
||||
|
||||
* fix `req.subdomains` when no Host is present
|
||||
* fix `req.host` when no Host is present, return undefined
|
||||
|
||||
3.2.3 / 2013-05-07
|
||||
==================
|
||||
|
||||
* update connect / qs
|
||||
|
||||
3.2.2 / 2013-05-03
|
||||
==================
|
||||
|
||||
* update qs
|
||||
|
||||
3.2.1 / 2013-04-29
|
||||
==================
|
||||
|
||||
* add app.VERB() paths array deprecation warning
|
||||
* update connect
|
||||
* update qs and remove all ~ semver crap
|
||||
* fix: accept number as value of Signed Cookie
|
||||
|
||||
3.2.0 / 2013-04-15
|
||||
==================
|
||||
|
||||
* add "view" constructor setting to override view behaviour
|
||||
* add req.acceptsEncoding(name)
|
||||
* add req.acceptedEncodings
|
||||
* revert cookie signature change causing session race conditions
|
||||
* fix sorting of Accept values of the same quality
|
||||
|
||||
3.1.2 / 2013-04-12
|
||||
==================
|
||||
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2009-2011 TJ Holowaychuk <tj@vision-media.ca>
|
||||
Copyright (c) 2009-2013 TJ Holowaychuk <tj@vision-media.ca>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||

|
||||
|
||||
Fast, unopinionated, minimalist web framework for [node](http://nodejs.org). [](http://travis-ci.org/visionmedia/express) [](https://gemnasium.com/visionmedia/express)
|
||||
Fast, unopinionated, minimalist web framework for [node](http://nodejs.org). [](http://travis-ci.org/visionmedia/express)
|
||||
|
||||
```js
|
||||
var express = require('express');
|
||||
@@ -64,7 +64,6 @@ app.listen(3000);
|
||||
* [Google Group](http://groups.google.com/group/express-js) for discussion
|
||||
* Follow [tjholowaychuk](http://twitter.com/tjholowaychuk) on twitter for updates
|
||||
* Visit the [Wiki](http://github.com/visionmedia/express/wiki)
|
||||
* [日本語ドキュメンテーション](http://hideyukisaito.com/doc/expressjs/) by [hideyukisaito](https://github.com/hideyukisaito)
|
||||
* [Русскоязычная документация](http://jsman.ru/express/)
|
||||
* Run express examples [online](https://runnable.com/express)
|
||||
|
||||
|
||||
17
bin/express
17
bin/express
@@ -16,6 +16,7 @@ var exec = require('child_process').exec
|
||||
|
||||
program
|
||||
.version(version)
|
||||
.usage('[options] [dir]')
|
||||
.option('-s, --sessions', 'add session support')
|
||||
.option('-e, --ejs', 'add ejs engine support (defaults to jade)')
|
||||
.option('-J, --jshtml', 'add jshtml engine support (defaults to jade)')
|
||||
@@ -208,11 +209,11 @@ var app = [
|
||||
, ' * Module dependencies.'
|
||||
, ' */'
|
||||
, ''
|
||||
, 'var express = require(\'express\')'
|
||||
, ' , routes = require(\'./routes\')'
|
||||
, ' , user = require(\'./routes/user\')'
|
||||
, ' , http = require(\'http\')'
|
||||
, ' , path = require(\'path\');'
|
||||
, 'var express = require(\'express\');'
|
||||
, 'var routes = require(\'./routes\');'
|
||||
, 'var user = require(\'./routes/user\');'
|
||||
, 'var http = require(\'http\');'
|
||||
, 'var path = require(\'path\');'
|
||||
, ''
|
||||
, 'var app = express();'
|
||||
, ''
|
||||
@@ -323,10 +324,10 @@ function createApplicationAt(path) {
|
||||
// CSS Engine support
|
||||
switch (program.css) {
|
||||
case 'less':
|
||||
app = app.replace('{css}', eol + ' app.use(require(\'less-middleware\')({ src: __dirname + \'/public\' }));');
|
||||
app = app.replace('{css}', eol + 'app.use(require(\'less-middleware\')({ src: __dirname + \'/public\' }));');
|
||||
break;
|
||||
case 'stylus':
|
||||
app = app.replace('{css}', eol + ' app.use(require(\'stylus\').middleware(__dirname + \'/public\'));');
|
||||
app = app.replace('{css}', eol + 'app.use(require(\'stylus\').middleware(__dirname + \'/public\'));');
|
||||
break;
|
||||
default:
|
||||
app = app.replace('{css}', '');
|
||||
@@ -334,7 +335,7 @@ function createApplicationAt(path) {
|
||||
|
||||
// Session support
|
||||
app = app.replace('{sess}', program.sessions
|
||||
? eol + ' app.use(express.cookieParser(\'your secret here\'));' + eol + ' app.use(express.session());'
|
||||
? eol + 'app.use(express.cookieParser(\'your secret here\'));' + eol + 'app.use(express.session());'
|
||||
: '');
|
||||
|
||||
// Template support
|
||||
|
||||
25
client.js
25
client.js
@@ -1,25 +0,0 @@
|
||||
|
||||
var http = require('http');
|
||||
|
||||
var times = 50;
|
||||
|
||||
while (times--) {
|
||||
var req = http.request({
|
||||
port: 3000
|
||||
, method: 'POST'
|
||||
, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
});
|
||||
|
||||
req.on('response', function(res){
|
||||
console.log(res.statusCode);
|
||||
});
|
||||
|
||||
var n = 500000;
|
||||
while (n--) {
|
||||
req.write('foo=bar&bar=baz&');
|
||||
}
|
||||
|
||||
req.write('foo=bar&bar=baz');
|
||||
|
||||
req.end();
|
||||
}
|
||||
@@ -31,7 +31,9 @@ var iterations = 12000;
|
||||
|
||||
exports.hash = function (pwd, salt, fn) {
|
||||
if (3 == arguments.length) {
|
||||
crypto.pbkdf2(pwd, salt, iterations, len, fn);
|
||||
crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){
|
||||
fn(err, (new Buffer(hash, 'binary')).toString('base64'));
|
||||
});
|
||||
} else {
|
||||
fn = salt;
|
||||
crypto.randomBytes(len, function(err, salt){
|
||||
@@ -39,8 +41,8 @@ exports.hash = function (pwd, salt, fn) {
|
||||
salt = salt.toString('base64');
|
||||
crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){
|
||||
if (err) return fn(err);
|
||||
fn(null, salt, hash);
|
||||
fn(null, salt, (new Buffer(hash, 'binary')).toString('base64'));
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
style
|
||||
style.
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 16px "Helvetica Neue", Helvetica;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
html
|
||||
head
|
||||
title Express
|
||||
script
|
||||
script.
|
||||
// call this whatever you like,
|
||||
// or dump them into individual
|
||||
// props like "var user ="
|
||||
@@ -10,5 +10,5 @@ html
|
||||
h1 Expose client data
|
||||
p The following was exposed to the client:
|
||||
pre
|
||||
script
|
||||
script.
|
||||
document.write(JSON.stringify(data, null, 2))
|
||||
@@ -3,9 +3,9 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../')
|
||||
var express = require('../..')
|
||||
, fs = require('fs')
|
||||
, md = require('github-flavored-markdown').parse;
|
||||
, md = require('marked').parse;
|
||||
|
||||
var app = module.exports = express();
|
||||
|
||||
@@ -42,4 +42,4 @@ app.get('/fail', function(req, res){
|
||||
if (!module.parent) {
|
||||
app.listen(3000);
|
||||
console.log('Express started on port 3000');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
var express = require('../..');
|
||||
|
||||
var app = module.exports = express();
|
||||
@@ -58,10 +57,10 @@ app.use(function(req, res, next){
|
||||
});
|
||||
*/
|
||||
|
||||
next();
|
||||
// empty or "flush" the messages so they
|
||||
// don't build up
|
||||
req.session.messages = [];
|
||||
next();
|
||||
});
|
||||
|
||||
// load controllers
|
||||
@@ -90,4 +89,4 @@ app.use(function(req, res, next){
|
||||
if (!module.parent) {
|
||||
app.listen(3000);
|
||||
console.log('\n listening on port 3000\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
html
|
||||
head
|
||||
title Search example
|
||||
style
|
||||
style.
|
||||
body {
|
||||
font: 14px "Helvetica Neue", Helvetica;
|
||||
padding: 50px;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
@@ -6,14 +5,14 @@
|
||||
var express = require('../..');
|
||||
|
||||
/*
|
||||
edit /etc/vhosts:
|
||||
edit /etc/hosts:
|
||||
|
||||
127.0.0.1 foo.example.com
|
||||
127.0.0.1 bar.example.com
|
||||
127.0.0.1 example.com
|
||||
*/
|
||||
|
||||
// Main app
|
||||
// Main server app
|
||||
|
||||
var main = express();
|
||||
|
||||
@@ -36,12 +35,12 @@ redirect.all('*', function(req, res){
|
||||
res.redirect('http://example.com:3000/' + req.subdomains[0]);
|
||||
});
|
||||
|
||||
// Main app
|
||||
// Vhost app
|
||||
|
||||
var app = express();
|
||||
|
||||
app.use(express.vhost('*.example.com', redirect))
|
||||
app.use(express.vhost('example.com', main));
|
||||
app.use(express.vhost('*.example.com', redirect)) // Serves all subdomains via Redirect app
|
||||
app.use(express.vhost('example.com', main)); // Serves top level domain via Main server app
|
||||
|
||||
app.listen(3000);
|
||||
console.log('Express app started on port 3000');
|
||||
|
||||
52
examples/view-constructor/github-view.js
Normal file
52
examples/view-constructor/github-view.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var http = require('http')
|
||||
, path = require('path')
|
||||
, extname = path.extname
|
||||
|
||||
/**
|
||||
* Expose `GithubView`.
|
||||
*/
|
||||
|
||||
module.exports = GithubView;
|
||||
|
||||
/**
|
||||
* Custom view that fetches and renders
|
||||
* remove github templates. You could
|
||||
* render templates from a database etc.
|
||||
*/
|
||||
|
||||
function GithubView(name, options){
|
||||
this.name = name;
|
||||
options = options || {};
|
||||
this.engine = options.engines[extname(name)];
|
||||
// "root" is the app.set('views') setting, however
|
||||
// in your own implementation you could ignore this
|
||||
this.path = '/' + options.root + '/master/' + name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the view.
|
||||
*/
|
||||
|
||||
GithubView.prototype.render = function(options, fn){
|
||||
var self = this;
|
||||
var opts = {
|
||||
host: 'rawgithub.com',
|
||||
port: 80,
|
||||
path: this.path,
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
http.request(opts, function(res) {
|
||||
var buf = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function(str){ buf += str });
|
||||
res.on('end', function(){
|
||||
self.engine(buf, options, fn);
|
||||
});
|
||||
}).end();
|
||||
};
|
||||
47
examples/view-constructor/index.js
Normal file
47
examples/view-constructor/index.js
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../')
|
||||
, http = require('http')
|
||||
, GithubView = require('./github-view')
|
||||
, md = require('marked').parse;
|
||||
|
||||
var app = module.exports = express();
|
||||
|
||||
// register .md as an engine in express view system
|
||||
app.engine('md', function(str, options, fn){
|
||||
try {
|
||||
var html = md(str);
|
||||
html = html.replace(/\{([^}]+)\}/g, function(_, name){
|
||||
return options[name] || '';
|
||||
})
|
||||
fn(null, html);
|
||||
} catch(err) {
|
||||
fn(err);
|
||||
}
|
||||
})
|
||||
|
||||
// pointing to a particular github repo to load files from it
|
||||
app.set('views', 'visionmedia/express');
|
||||
|
||||
// register a new view constructor
|
||||
app.set('view', GithubView);
|
||||
|
||||
app.get('/', function(req, res){
|
||||
// rendering a view relative to the repo.
|
||||
// app.locals, res.locals, and locals passed
|
||||
// work like they normally would
|
||||
res.render('examples/markdown/views/index.md', { title: 'Example' });
|
||||
})
|
||||
|
||||
app.get('/Readme.md', function(req, res){
|
||||
// rendering a view from https://github.com/visionmedia/express/blob/master/Readme.md
|
||||
res.render('Readme.md');
|
||||
})
|
||||
|
||||
if (!module.parent) {
|
||||
app.listen(3000);
|
||||
console.log('Express started on port 3000');
|
||||
}
|
||||
@@ -2,7 +2,7 @@ doctype 5
|
||||
html
|
||||
head
|
||||
title= title
|
||||
style
|
||||
style.
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 16px Helvetica, Arial;
|
||||
|
||||
@@ -46,6 +46,7 @@ app.init = function(){
|
||||
app.defaultConfiguration = function(){
|
||||
// default settings
|
||||
this.enable('x-powered-by');
|
||||
this.enable('etag');
|
||||
this.set('env', process.env.NODE_ENV || 'development');
|
||||
this.set('subdomain offset', 2);
|
||||
debug('booting in %s mode', this.get('env'));
|
||||
@@ -79,6 +80,7 @@ app.defaultConfiguration = function(){
|
||||
this.locals.settings = this.settings;
|
||||
|
||||
// default configuration
|
||||
this.set('view', View);
|
||||
this.set('views', process.cwd() + '/views');
|
||||
this.set('jsonp callback name', 'callback');
|
||||
|
||||
@@ -116,7 +118,6 @@ app.use = function(route, fn){
|
||||
fn = function(req, res, next) {
|
||||
var orig = req.app;
|
||||
app.handle(req, res, function(err){
|
||||
req.app = res.app = orig;
|
||||
req.__proto__ = orig.request;
|
||||
res.__proto__ = orig.response;
|
||||
next(err);
|
||||
@@ -184,7 +185,7 @@ app.engine = function(ext, fn){
|
||||
* could automatically load a user's information from the database without
|
||||
* any additional code,
|
||||
*
|
||||
* The callback uses the samesignature as middleware, the only differencing
|
||||
* The callback uses the same signature as middleware, the only differencing
|
||||
* being that the value of the placeholder is passed, in this case the _id_
|
||||
* of the user. Once the `next()` function is invoked, just like middleware
|
||||
* it will continue on to execute the route, or subsequent parameter functions.
|
||||
@@ -401,6 +402,11 @@ methods.forEach(function(method){
|
||||
app[method] = function(path){
|
||||
if ('get' == method && 1 == arguments.length) return this.set(path);
|
||||
|
||||
// deprecated
|
||||
if (Array.isArray(path)) {
|
||||
console.trace('passing an array to app.VERB() is deprecated and will be removed in 4.0');
|
||||
}
|
||||
|
||||
// if no router attached yet, attach the router
|
||||
if (!this._usedRouter) this.use(this.router);
|
||||
|
||||
@@ -479,7 +485,7 @@ app.render = function(name, options, fn){
|
||||
|
||||
// view
|
||||
if (!view) {
|
||||
view = new View(name, {
|
||||
view = new (this.get('view'))(name, {
|
||||
defaultEngine: this.get('view engine'),
|
||||
root: this.get('views'),
|
||||
engines: engines
|
||||
|
||||
@@ -16,12 +16,6 @@ var connect = require('connect')
|
||||
|
||||
exports = module.exports = createApplication;
|
||||
|
||||
/**
|
||||
* Framework version.
|
||||
*/
|
||||
|
||||
exports.version = '3.1.2';
|
||||
|
||||
/**
|
||||
* Expose mime.
|
||||
*/
|
||||
@@ -38,8 +32,8 @@ exports.mime = connect.mime;
|
||||
function createApplication() {
|
||||
var app = connect();
|
||||
utils.merge(app, proto);
|
||||
app.request = { __proto__: req };
|
||||
app.response = { __proto__: res };
|
||||
app.request = { __proto__: req, app: app };
|
||||
app.response = { __proto__: res, app: app };
|
||||
app.init();
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ var utils = require('./utils');
|
||||
|
||||
exports.init = function(app){
|
||||
return function expressInit(req, res, next){
|
||||
req.app = res.app = app;
|
||||
if (app.enabled('x-powered-by')) res.setHeader('X-Powered-By', 'Express');
|
||||
req.res = res;
|
||||
res.req = req;
|
||||
|
||||
@@ -63,6 +63,7 @@ req.header = function(name){
|
||||
* The `type` value may be a single mime type string
|
||||
* such as "application/json", the extension name
|
||||
* such as "json", a comma-delimted list such as "json, html, text/plain",
|
||||
* an argument list such as `"json", "html", "text/plain"`,
|
||||
* or an array `["json", "html", "text/plain"]`. When a list
|
||||
* or array is given the _best_ match, if any is returned.
|
||||
*
|
||||
@@ -89,6 +90,7 @@ req.header = function(name){
|
||||
*
|
||||
* // Accept: text/*;q=.5, application/json
|
||||
* req.accepts(['html', 'json']);
|
||||
* req.accepts('html', 'json');
|
||||
* req.accepts('html, json');
|
||||
* // => "json"
|
||||
*
|
||||
@@ -98,7 +100,20 @@ req.header = function(name){
|
||||
*/
|
||||
|
||||
req.accepts = function(type){
|
||||
return utils.accepts(type, this.get('Accept'));
|
||||
var args = arguments.length > 1 ? [].slice.apply(arguments) : type;
|
||||
return utils.accepts(args, this.get('Accept'));
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the given `encoding` is accepted.
|
||||
*
|
||||
* @param {String} encoding
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.acceptsEncoding = function(encoding){
|
||||
return !! ~this.acceptedEncodings.indexOf(encoding);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -113,7 +128,7 @@ req.accepts = function(type){
|
||||
req.acceptsCharset = function(charset){
|
||||
var accepted = this.acceptedCharsets;
|
||||
return accepted.length
|
||||
? ~accepted.indexOf(charset)
|
||||
? !! ~accepted.indexOf(charset)
|
||||
: true;
|
||||
};
|
||||
|
||||
@@ -129,7 +144,7 @@ req.acceptsCharset = function(charset){
|
||||
req.acceptsLanguage = function(lang){
|
||||
var accepted = this.acceptedLanguages;
|
||||
return accepted.length
|
||||
? ~accepted.indexOf(lang)
|
||||
? !! ~accepted.indexOf(lang)
|
||||
: true;
|
||||
};
|
||||
|
||||
@@ -159,6 +174,24 @@ req.range = function(size){
|
||||
return parseRange(size, range);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return an array of encodings.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* ['gzip', 'deflate']
|
||||
*
|
||||
* @return {Array}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.__defineGetter__('acceptedEncodings', function(){
|
||||
var accept = this.get('Accept-Encoding');
|
||||
return accept
|
||||
? accept.trim().split(/ *, */)
|
||||
: [];
|
||||
});
|
||||
|
||||
/**
|
||||
* Return an array of Accepted media types
|
||||
* ordered from highest quality to lowest.
|
||||
@@ -315,11 +348,10 @@ req.is = function(type){
|
||||
|
||||
req.__defineGetter__('protocol', function(){
|
||||
var trustProxy = this.app.get('trust proxy');
|
||||
return this.connection.encrypted
|
||||
? 'https'
|
||||
: trustProxy
|
||||
? (this.get('X-Forwarded-Proto') || 'http')
|
||||
: 'http';
|
||||
if (this.connection.encrypted) return 'https';
|
||||
if (!trustProxy) return 'http';
|
||||
var proto = this.get('X-Forwarded-Proto') || 'http';
|
||||
return proto.split(/\s*,\s*/)[0];
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -415,7 +447,7 @@ req.__defineGetter__('auth', function(){
|
||||
|
||||
req.__defineGetter__('subdomains', function(){
|
||||
var offset = this.app.get('subdomain offset');
|
||||
return this.get('Host')
|
||||
return (this.host || '')
|
||||
.split('.')
|
||||
.reverse()
|
||||
.slice(offset);
|
||||
@@ -443,6 +475,7 @@ req.__defineGetter__('host', function(){
|
||||
var trustProxy = this.app.get('trust proxy');
|
||||
var host = trustProxy && this.get('X-Forwarded-Host');
|
||||
host = host || this.get('Host');
|
||||
if (!host) return;
|
||||
return host.split(':')[0];
|
||||
});
|
||||
|
||||
|
||||
@@ -78,9 +78,12 @@ res.links = function(links){
|
||||
*/
|
||||
|
||||
res.send = function(body){
|
||||
var req = this.req
|
||||
, head = 'HEAD' == req.method
|
||||
, len;
|
||||
var req = this.req;
|
||||
var head = 'HEAD' == req.method;
|
||||
var len;
|
||||
|
||||
// settings
|
||||
var app = this.app;
|
||||
|
||||
// allow status / body
|
||||
if (2 == arguments.length) {
|
||||
@@ -128,7 +131,7 @@ res.send = function(body){
|
||||
|
||||
// ETag support
|
||||
// TODO: W/ support
|
||||
if (len > 1024) {
|
||||
if (app.settings.etag && len > 1024 && 'GET' == req.method) {
|
||||
if (!this.get('ETag')) {
|
||||
this.set('ETag', etag(body));
|
||||
}
|
||||
@@ -234,6 +237,7 @@ res.jsonp = function(obj){
|
||||
|
||||
// jsonp
|
||||
if (callback) {
|
||||
if (callback instanceof Array) callback = callback[0];
|
||||
this.set('Content-Type', 'text/javascript');
|
||||
var cb = callback.replace(/[^\[\]\w$.]/g, '');
|
||||
body = cb + ' && ' + cb + '(' + body + ');';
|
||||
@@ -580,6 +584,7 @@ res.cookie = function(name, val, options){
|
||||
var secret = this.req.secret;
|
||||
var signed = options.signed;
|
||||
if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');
|
||||
if ('number' == typeof val) val = val.toString();
|
||||
if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
|
||||
if (signed) val = 's:' + sign(val, secret);
|
||||
if ('maxAge' in options) {
|
||||
|
||||
@@ -103,6 +103,9 @@ Router.prototype._dispatch = function(req, res, next){
|
||||
// match route
|
||||
req.route = route = self.matchRequest(req, i);
|
||||
|
||||
// implied OPTIONS
|
||||
if (!route && 'OPTIONS' == req.method) return self._options(req, res);
|
||||
|
||||
// no route
|
||||
if (!route) return next(err);
|
||||
debug('matched %s %s', route.method, route.path);
|
||||
@@ -170,6 +173,41 @@ Router.prototype._dispatch = function(req, res, next){
|
||||
})(0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Respond to __OPTIONS__ method.
|
||||
*
|
||||
* @param {IncomingMessage} req
|
||||
* @param {ServerResponse} res
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Router.prototype._options = function(req, res){
|
||||
var path = parse(req).pathname
|
||||
, body = this._optionsFor(path).join(',');
|
||||
res.set('Allow', body).send(body);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return an array of HTTP verbs or "options" for `path`.
|
||||
*
|
||||
* @param {String} path
|
||||
* @return {Array}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Router.prototype._optionsFor = function(path){
|
||||
var self = this;
|
||||
return methods.filter(function(method){
|
||||
var routes = self.map[method];
|
||||
if (!routes || 'options' == method) return;
|
||||
for (var i = 0, len = routes.length; i < len; ++i) {
|
||||
if (routes[i].match(path)) return true;
|
||||
}
|
||||
}).map(function(method){
|
||||
return method.toUpperCase();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Attempt to match a route for `req`
|
||||
* with optional starting index of `i`
|
||||
|
||||
19
lib/utils.js
19
lib/utils.js
@@ -6,6 +6,12 @@
|
||||
var mime = require('connect').mime
|
||||
, crc32 = require('buffer-crc32');
|
||||
|
||||
/**
|
||||
* toString ref.
|
||||
*/
|
||||
|
||||
var toString = {}.toString;
|
||||
|
||||
/**
|
||||
* Return ETag for `body`.
|
||||
*
|
||||
@@ -217,22 +223,27 @@ exports.parseParams = function(str){
|
||||
return obj.quality;
|
||||
})
|
||||
.sort(function(a, b){
|
||||
return b.quality - a.quality;
|
||||
if (a.quality === b.quality) {
|
||||
return a.originalIndex - b.originalIndex;
|
||||
} else {
|
||||
return b.quality - a.quality;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse accept params `str` returning an
|
||||
* object with `.value`, `.quality` and `.params`.
|
||||
* also includes `.originalIndex` for stable sorting
|
||||
*
|
||||
* @param {String} str
|
||||
* @return {Object}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function acceptParams(str) {
|
||||
function acceptParams(str, index) {
|
||||
var parts = str.split(/ *; */);
|
||||
var ret = { value: parts[0], quality: 1, params: {} };
|
||||
var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };
|
||||
|
||||
for (var i = 1; i < parts.length; ++i) {
|
||||
var pms = parts[i].split(/ *= */);
|
||||
@@ -280,7 +291,7 @@ exports.escape = function(html) {
|
||||
*/
|
||||
|
||||
exports.pathRegexp = function(path, keys, sensitive, strict) {
|
||||
if (path instanceof RegExp) return path;
|
||||
if (toString.call(path) == '[object RegExp]') return path;
|
||||
if (Array.isArray(path)) path = '(' + path.join('|') + ')';
|
||||
path = path
|
||||
.concat(strict ? '' : '/?')
|
||||
|
||||
@@ -38,6 +38,7 @@ function View(name, options) {
|
||||
var engines = options.engines;
|
||||
this.defaultEngine = options.defaultEngine;
|
||||
var ext = this.ext = extname(name);
|
||||
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
|
||||
if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
|
||||
this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
|
||||
this.path = this.lookup(name);
|
||||
|
||||
24
package.json
24
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "express",
|
||||
"description": "Sinatra inspired web development framework",
|
||||
"version": "3.1.2",
|
||||
"version": "3.3.8",
|
||||
"author": "TJ Holowaychuk <tj@vision-media.ca>",
|
||||
"contributors": [
|
||||
{
|
||||
@@ -22,28 +22,28 @@
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"connect": "2.7.5",
|
||||
"commander": "0.6.1",
|
||||
"connect": "2.8.8",
|
||||
"commander": "1.2.0",
|
||||
"range-parser": "0.0.4",
|
||||
"mkdirp": "~0.3.4",
|
||||
"cookie": "0.0.5",
|
||||
"buffer-crc32": "~0.2.1",
|
||||
"fresh": "0.1.0",
|
||||
"mkdirp": "0.3.5",
|
||||
"cookie": "0.1.0",
|
||||
"buffer-crc32": "0.2.1",
|
||||
"fresh": "0.2.0",
|
||||
"methods": "0.0.1",
|
||||
"send": "0.1.0",
|
||||
"cookie-signature": "1.0.0",
|
||||
"send": "0.1.4",
|
||||
"cookie-signature": "1.0.1",
|
||||
"debug": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ejs": "*",
|
||||
"mocha": "*",
|
||||
"jade": "*",
|
||||
"jade": "0.30.0",
|
||||
"hjs": "*",
|
||||
"stylus": "*",
|
||||
"should": "*",
|
||||
"connect-redis": "*",
|
||||
"github-flavored-markdown": "*",
|
||||
"supertest": "0.0.1"
|
||||
"marked": "*",
|
||||
"supertest": "0.6.0"
|
||||
},
|
||||
"keywords": [
|
||||
"express",
|
||||
|
||||
37
test/app.options.js
Normal file
37
test/app.options.js
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
|
||||
describe('OPTIONS', function(){
|
||||
it('should default to the routes defined', function(done){
|
||||
var app = express();
|
||||
|
||||
app.del('/', function(){});
|
||||
app.get('/users', function(req, res){});
|
||||
app.put('/users', function(req, res){});
|
||||
|
||||
request(app)
|
||||
.options('/users')
|
||||
.expect('GET,PUT')
|
||||
.expect('Allow', 'GET,PUT', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('app.options()', function(){
|
||||
it('should override the default behavior', function(done){
|
||||
var app = express();
|
||||
|
||||
app.options('/users', function(req, res){
|
||||
res.set('Allow', 'GET');
|
||||
res.send('GET');
|
||||
});
|
||||
|
||||
app.get('/users', function(req, res){});
|
||||
app.put('/users', function(req, res){});
|
||||
|
||||
request(app)
|
||||
.options('/users')
|
||||
.expect('GET')
|
||||
.expect('Allow', 'GET', done);
|
||||
})
|
||||
})
|
||||
@@ -8,7 +8,7 @@ describe('app', function(){
|
||||
var app = express();
|
||||
|
||||
app.param(function(name, regexp){
|
||||
if (regexp instanceof RegExp) {
|
||||
if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557
|
||||
return function(req, res, next, val){
|
||||
var captures;
|
||||
if (captures = regexp.exec(String(val))) {
|
||||
|
||||
@@ -14,7 +14,7 @@ describe('app', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should support absolute paths with "view engine"', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -40,7 +40,7 @@ describe('app', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should support index.<engine>', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -109,8 +109,31 @@ describe('app', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when a "view" constructor is given', function(){
|
||||
it('should create an instance of it', function(done){
|
||||
var app = express();
|
||||
|
||||
function View(name, options){
|
||||
this.name = name;
|
||||
this.path = 'path is required by application.js as a signal of success even though it is not used there.';
|
||||
}
|
||||
|
||||
View.prototype.render = function(options, fn){
|
||||
fn(null, 'abstract engine');
|
||||
};
|
||||
|
||||
app.set('view', View);
|
||||
|
||||
app.render('something', function(err, str){
|
||||
if (err) return done(err);
|
||||
str.should.equal('abstract engine');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.render(name, options, fn)', function(){
|
||||
it('should render the template', function(done){
|
||||
var app = express();
|
||||
@@ -125,7 +148,7 @@ describe('app', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should expose app.locals', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -138,7 +161,7 @@ describe('app', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should give precedence to app.render() locals', function(done){
|
||||
var app = express();
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ describe('app.router', function(){
|
||||
calls.push('before');
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
app.use(app.router);
|
||||
|
||||
app.use(function(req, res, next){
|
||||
@@ -68,7 +68,7 @@ describe('app.router', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should be auto .use()d on the first app.VERB() call', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -78,7 +78,7 @@ describe('app.router', function(){
|
||||
calls.push('before');
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
app.get('/', function(req, res, next){
|
||||
calls.push('GET /')
|
||||
next();
|
||||
@@ -109,7 +109,7 @@ describe('app.router', function(){
|
||||
.get('/user/12?foo=bar')
|
||||
.expect('user', done);
|
||||
})
|
||||
|
||||
|
||||
it('should populate req.params with the captures', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -124,24 +124,6 @@ describe('app.router', function(){
|
||||
.expect('editing user 10', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('when given an array', function(){
|
||||
it('should match all paths in the array', function(done){
|
||||
var app = express();
|
||||
|
||||
app.get(['/one', '/two'], function(req, res){
|
||||
res.end('works');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/one')
|
||||
.expect('works', function() {
|
||||
request(app)
|
||||
.get('/two')
|
||||
.expect('works', done);
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe('case sensitivity', function(){
|
||||
it('should be disabled by default', function(done){
|
||||
@@ -155,7 +137,7 @@ describe('app.router', function(){
|
||||
.get('/USER')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
describe('when "case sensitive routing" is enabled', function(){
|
||||
it('should match identical casing', function(done){
|
||||
var app = express();
|
||||
@@ -170,7 +152,7 @@ describe('app.router', function(){
|
||||
.get('/uSer')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should not match otherwise', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -199,7 +181,7 @@ describe('app.router', function(){
|
||||
.get('/user/')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
describe('when "strict routing" is enabled', function(){
|
||||
it('should match trailing slashes', function(done){
|
||||
var app = express();
|
||||
@@ -214,7 +196,7 @@ describe('app.router', function(){
|
||||
.get('/user/')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should match no slashes', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -228,7 +210,7 @@ describe('app.router', function(){
|
||||
.get('/user')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should fail when omitting the trailing slash', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -242,7 +224,7 @@ describe('app.router', function(){
|
||||
.get('/user')
|
||||
.expect(404, done);
|
||||
})
|
||||
|
||||
|
||||
it('should fail when adding the trailing slash', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -275,7 +257,7 @@ describe('app.router', function(){
|
||||
.expect(404, done);
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
it('should allow literal "."', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -303,7 +285,7 @@ describe('app.router', function(){
|
||||
.get('/user/tj.json')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should work with several', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -346,7 +328,7 @@ describe('app.router', function(){
|
||||
.get('/api/users/0.json')
|
||||
.expect('users/0.json', done);
|
||||
})
|
||||
|
||||
|
||||
it('should not be greedy immediately after param', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -370,7 +352,7 @@ describe('app.router', function(){
|
||||
.get('/user/122/aaa')
|
||||
.expect('122', done);
|
||||
})
|
||||
|
||||
|
||||
it('should span multiple segments', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -382,7 +364,7 @@ describe('app.router', function(){
|
||||
.get('/file/javascripts/jquery.js')
|
||||
.expect('javascripts/jquery.js', done);
|
||||
})
|
||||
|
||||
|
||||
it('should be optional', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -394,7 +376,7 @@ describe('app.router', function(){
|
||||
.get('/file/')
|
||||
.expect('', done);
|
||||
})
|
||||
|
||||
|
||||
it('should require a preceeding /', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -420,7 +402,7 @@ describe('app.router', function(){
|
||||
.get('/user/tj')
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should match a single segment only', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -432,7 +414,7 @@ describe('app.router', function(){
|
||||
.get('/user/tj/edit')
|
||||
.expect(404, done);
|
||||
})
|
||||
|
||||
|
||||
it('should allow several capture groups', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -459,7 +441,7 @@ describe('app.router', function(){
|
||||
.get('/user/tj')
|
||||
.expect('viewing tj', done);
|
||||
})
|
||||
|
||||
|
||||
it('should populate the capture group', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -473,7 +455,7 @@ describe('app.router', function(){
|
||||
.expect('editing tj', done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.:name', function(){
|
||||
it('should denote a format', function(done){
|
||||
var app = express();
|
||||
@@ -491,7 +473,7 @@ describe('app.router', function(){
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.:name?', function(){
|
||||
it('should denote an optional format', function(done){
|
||||
var app = express();
|
||||
@@ -509,7 +491,7 @@ describe('app.router', function(){
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when next() is called', function(){
|
||||
it('should continue lookup', function(done){
|
||||
var app = express()
|
||||
@@ -528,7 +510,7 @@ describe('app.router', function(){
|
||||
calls.push('/foo');
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
app.get('/foo', function(req, res, next){
|
||||
calls.push('/foo 2');
|
||||
res.end('done');
|
||||
@@ -542,7 +524,7 @@ describe('app.router', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when next(err) is called', function(){
|
||||
it('should break out of app.router', function(done){
|
||||
var app = express()
|
||||
@@ -561,7 +543,7 @@ describe('app.router', function(){
|
||||
calls.push('/foo');
|
||||
next(new Error('fail'));
|
||||
});
|
||||
|
||||
|
||||
app.get('/foo', function(req, res, next){
|
||||
assert(0);
|
||||
});
|
||||
|
||||
@@ -4,10 +4,6 @@ var express = require('../')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('exports', function(){
|
||||
it('should have .version', function(){
|
||||
express.should.have.property('version');
|
||||
})
|
||||
|
||||
it('should expose connect middleware', function(){
|
||||
express.should.have.property('bodyParser');
|
||||
express.should.have.property('session');
|
||||
@@ -21,15 +17,15 @@ describe('exports', function(){
|
||||
it('should expose Router', function(){
|
||||
express.Router.should.be.a('function');
|
||||
})
|
||||
|
||||
|
||||
it('should expose the application prototype', function(){
|
||||
express.application.set.should.be.a('function');
|
||||
})
|
||||
|
||||
|
||||
it('should expose the request prototype', function(){
|
||||
express.request.accepts.should.be.a('function');
|
||||
})
|
||||
|
||||
|
||||
it('should expose the response prototype', function(){
|
||||
express.response.send.should.be.a('function');
|
||||
})
|
||||
@@ -51,7 +47,7 @@ describe('exports', function(){
|
||||
.get('/')
|
||||
.expect('bar', done);
|
||||
})
|
||||
|
||||
|
||||
it('should permit modifying the .response prototype', function(done){
|
||||
express.response.foo = function(){ this.send('bar'); };
|
||||
var app = express();
|
||||
|
||||
36
test/req.acceptedEncodings.js
Normal file
36
test/req.acceptedEncodings.js
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
|
||||
describe('req', function(){
|
||||
describe('.acceptedEncodings', function(){
|
||||
it('should return an array of accepted encodings', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
req.acceptedEncodings.should.eql(['gzip', 'deflate']);
|
||||
res.end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept-Encoding', ' gzip, deflate')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
describe('when Accept-Encoding is not present', function(){
|
||||
it('should default to []', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
req.acceptedEncodings.should.have.length(0);
|
||||
res.end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -56,6 +56,19 @@ describe('req', function(){
|
||||
.expect('html', done);
|
||||
})
|
||||
|
||||
it('should accept an argument list of type names', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.end(req.accepts('json', 'html'));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('json', done);
|
||||
})
|
||||
|
||||
describe('.accept(types)', function(){
|
||||
it('should return the first when Accept is not present', function(done){
|
||||
var app = express();
|
||||
|
||||
34
test/req.host.js
Normal file
34
test/req.host.js
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('req', function(){
|
||||
describe('.host', function(){
|
||||
it('should return the Host when present', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.end(req.host);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('Host', 'example.com')
|
||||
.expect('example.com', done);
|
||||
})
|
||||
|
||||
it('should return undefined otherwise', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
req.headers.host = null;
|
||||
res.end(String(req.host));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.post('/')
|
||||
.expect('undefined', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
83
test/req.secure.js
Normal file
83
test/req.secure.js
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
|
||||
describe('req', function(){
|
||||
describe('.secure', function(){
|
||||
describe('when X-Forwarded-Proto is missing', function(){
|
||||
it('should return false when http', function(done){
|
||||
var app = express();
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send(req.secure ? 'yes' : 'no');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('no', done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.secure', function(){
|
||||
describe('when X-Forwarded-Proto is present', function(){
|
||||
it('should return false when http', function(done){
|
||||
var app = express();
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send(req.secure ? 'yes' : 'no');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'https')
|
||||
.expect('no', done)
|
||||
})
|
||||
|
||||
it('should return true when "trust proxy" is enabled', function(done){
|
||||
var app = express();
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send(req.secure ? 'yes' : 'no');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'https')
|
||||
.expect('yes', done)
|
||||
})
|
||||
|
||||
it('should return false when initial proxy is http', function(done){
|
||||
var app = express();
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send(req.secure ? 'yes' : 'no');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'http, https')
|
||||
.expect('no', done)
|
||||
})
|
||||
|
||||
it('should return true when initial proxy is https', function(done){
|
||||
var app = express();
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send(req.secure ? 'yes' : 'no');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'https, http')
|
||||
.expect('yes', done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -34,6 +34,21 @@ describe('req', function(){
|
||||
})
|
||||
})
|
||||
|
||||
describe('with no host', function(){
|
||||
it('should return an empty array', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
req.headers.host = null;
|
||||
res.send(req.subdomains);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('[]', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('when subdomain offset is set', function(){
|
||||
describe('when subdomain offset is zero', function(){
|
||||
it('should return an array with the whole domain', function(done){
|
||||
|
||||
@@ -39,7 +39,7 @@ describe('res', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
it('should allow multiple calls', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -58,7 +58,7 @@ describe('res', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.cookie(name, string, options)', function(){
|
||||
it('should set params', function(done){
|
||||
var app = express();
|
||||
@@ -76,7 +76,7 @@ describe('res', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('maxAge', function(){
|
||||
it('should set relative expires', function(done){
|
||||
var app = express();
|
||||
@@ -164,7 +164,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
var val = ['name=s%3Atobi.z6Nfs5haoP4UAqCcr497ZljiIbrNqn8EjjM10baIYvvG32lReGseC3v2T79fecY4G%2FLdEm9fpTihvlEMVuNjGw; Path=/'];
|
||||
var val = ['name=s%3Atobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
|
||||
@@ -53,7 +53,7 @@ app3.use(function(req, res, next){
|
||||
})
|
||||
});
|
||||
|
||||
describe('req', function(){
|
||||
describe('res', function(){
|
||||
describe('.format(obj)', function(){
|
||||
describe('with canonicalized mime types', function(){
|
||||
test(app);
|
||||
@@ -79,14 +79,14 @@ function test(app) {
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
|
||||
.expect('{"message":"hey"}', done);
|
||||
.expect({"message":"hey"}, done);
|
||||
})
|
||||
|
||||
it('should allow wildcard type/subtypes', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
|
||||
.expect('{"message":"hey"}', done);
|
||||
.expect({"message":"hey"}, done);
|
||||
})
|
||||
|
||||
it('should default the Content-Type', function(done){
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
, request = require('./support/http')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('res', function(){
|
||||
describe('.send(null)', function(){
|
||||
@@ -16,7 +17,7 @@ describe('res', function(){
|
||||
.expect('', done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.send(undefined)', function(){
|
||||
it('should set body to ""', function(done){
|
||||
var app = express();
|
||||
@@ -45,7 +46,7 @@ describe('res', function(){
|
||||
.expect(201, done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.send(code, body)', function(){
|
||||
it('should set .statusCode and body', function(done){
|
||||
var app = express();
|
||||
@@ -107,7 +108,24 @@ describe('res', function(){
|
||||
.expect('ETag', '"-1498647312"')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
|
||||
it('should not set ETag for non-GET/HEAD', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.post('/')
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
assert(!res.header.etag, 'has an ETag');
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('should not override Content-Type', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -122,7 +140,7 @@ describe('res', function(){
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.send(Buffer)', function(){
|
||||
it('should send as octet-stream', function(done){
|
||||
var app = express();
|
||||
@@ -172,7 +190,7 @@ describe('res', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.send(Object)', function(){
|
||||
it('should send as application/json', function(done){
|
||||
var app = express();
|
||||
@@ -224,7 +242,7 @@ describe('res', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when .statusCode is 304', function(){
|
||||
it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){
|
||||
var app = express();
|
||||
@@ -300,4 +318,62 @@ describe('res', function(){
|
||||
.get('/?callback=foo')
|
||||
.expect('{"foo":"bar"}', done);
|
||||
})
|
||||
|
||||
describe('"etag" setting', function(){
|
||||
describe('when enabled', function(){
|
||||
it('should send ETag ', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('etag', '"-1498647312"');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when disabled', function(){
|
||||
it('should send no ETag', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
app.disable('etag');
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.headers.should.not.have.property('etag');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send ETag when manually set', function(done){
|
||||
var app = express();
|
||||
|
||||
app.disable('etag');
|
||||
|
||||
app.use(function(req, res){
|
||||
res.set('etag', 1);
|
||||
res.send(200);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('etag');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ describe('utils.etag(body)', function(){
|
||||
|
||||
var str = 'Hello CRC';
|
||||
var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';
|
||||
|
||||
|
||||
it('should support strings', function(){
|
||||
utils.etag(str).should.eql('"-2034458343"');
|
||||
})
|
||||
@@ -27,7 +27,7 @@ describe('utils.isAbsolute()', function(){
|
||||
assert(utils.isAbsolute('c:\\'));
|
||||
assert(!utils.isAbsolute(':\\'));
|
||||
})
|
||||
|
||||
|
||||
it('should unices', function(){
|
||||
assert(utils.isAbsolute('/foo/bar'));
|
||||
assert(!utils.isAbsolute('foo/bar'));
|
||||
@@ -52,32 +52,32 @@ describe('utils.escape(html)', function(){
|
||||
describe('utils.parseParams(str)', function(){
|
||||
it('should default quality to 1', function(){
|
||||
utils.parseParams('text/html')
|
||||
.should.eql([{ value: 'text/html', quality: 1, params: {}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);
|
||||
})
|
||||
|
||||
|
||||
it('should parse qvalues', function(){
|
||||
utils.parseParams('text/html; q=0.5')
|
||||
.should.eql([{ value: 'text/html', quality: 0.5, params: {}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);
|
||||
|
||||
utils.parseParams('text/html; q=.2')
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);
|
||||
})
|
||||
|
||||
it('should parse accept parameters', function(){
|
||||
utils.parseParams('application/json; ver=2.0')
|
||||
.should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}}]);
|
||||
.should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);
|
||||
|
||||
utils.parseParams('text/html; q=0.5; level=2')
|
||||
.should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);
|
||||
utils.parseParams('text/html;q=.2;ver=beta')
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);
|
||||
})
|
||||
|
||||
it('should work with messed up whitespace', function(){
|
||||
utils.parseParams('text/html ; q = .2')
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);
|
||||
.should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);
|
||||
})
|
||||
|
||||
|
||||
it('should work with multiples', function(){
|
||||
var str = 'da, en;q=.5, en-gb;q=.8';
|
||||
var arr = utils.parseParams(str);
|
||||
@@ -85,7 +85,13 @@ describe('utils.parseParams(str)', function(){
|
||||
arr[1].value.should.equal('en-gb');
|
||||
arr[2].value.should.equal('en');
|
||||
})
|
||||
|
||||
|
||||
it('should work with long lists', function(){
|
||||
var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';
|
||||
var arr = utils.parseParams(str).map(function(o){ return o.value });
|
||||
arr.should.eql(str.split(', '));
|
||||
})
|
||||
|
||||
it('should sort by quality', function(){
|
||||
var str = 'text/plain;q=.2, application/json, text/html;q=0.5';
|
||||
var arr = utils.parseParams(str);
|
||||
@@ -93,7 +99,7 @@ describe('utils.parseParams(str)', function(){
|
||||
arr[1].value.should.equal('text/html');
|
||||
arr[2].value.should.equal('text/plain');
|
||||
})
|
||||
|
||||
|
||||
it('should exclude those with a quality of 0', function(){
|
||||
var str = 'text/plain;q=.2, application/json, text/html;q=0';
|
||||
var arr = utils.parseParams(str);
|
||||
@@ -106,7 +112,7 @@ describe('utils.parseAccept(str)', function(){
|
||||
var arr = utils.parseAccept('text/html');
|
||||
arr[0].type.should.equal('text');
|
||||
})
|
||||
|
||||
|
||||
it('should provide .subtype', function(){
|
||||
var arr = utils.parseAccept('text/html');
|
||||
arr[0].subtype.should.equal('html');
|
||||
@@ -120,7 +126,7 @@ describe('utils.accepts(type, str)', function(){
|
||||
.should.equal('text/html');
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when a string is empty', function(){
|
||||
it('should return the value', function(){
|
||||
utils.accepts('text/html', '')
|
||||
@@ -166,44 +172,44 @@ describe('utils.accepts(type, str)', function(){
|
||||
utils.accepts('text/html', 'text/plain, text/html')
|
||||
.should.equal('text/html');
|
||||
})
|
||||
|
||||
|
||||
it('should return undefined otherwise', function(){
|
||||
assert(null == utils.accepts('text/html', 'text/plain, application/json'));
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when accepting */subtype', function(){
|
||||
it('should return the value when present', function(){
|
||||
utils.accepts('text/html', 'text/*')
|
||||
.should.equal('text/html');
|
||||
})
|
||||
|
||||
|
||||
it('should return undefined otherwise', function(){
|
||||
assert(null == utils.accepts('text/html', 'image/*'));
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when accepting type/*', function(){
|
||||
it('should return the value when present', function(){
|
||||
utils.accepts('text/html', '*/html')
|
||||
.should.equal('text/html');
|
||||
})
|
||||
|
||||
|
||||
it('should return undefined otherwise', function(){
|
||||
assert(null == utils.accepts('text/html', '*/json'));
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when an extension is given', function(){
|
||||
it('should return the value when present', function(){
|
||||
utils.accepts('html', 'text/html, application/json')
|
||||
.should.equal('html');
|
||||
})
|
||||
|
||||
|
||||
it('should return undefined otherwise', function(){
|
||||
assert(null == utils.accepts('html', 'text/plain, application/json'));
|
||||
})
|
||||
|
||||
|
||||
it('should support *', function(){
|
||||
utils.accepts('html', 'text/*')
|
||||
.should.equal('html');
|
||||
@@ -212,4 +218,4 @@ describe('utils.accepts(type, str)', function(){
|
||||
.should.equal('html');
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user