Closes #88.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Merge branch 'runtime-routes'
authored by visionmedia · committed by visionmedia · parents 31e608e, 90d48b3 · Browse files · 6 files·+179 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+179 added−73 removed
- tests:+76 added−69 removed
- other:+103 added−4 removed
Provenance
- Commit
b8f54d6- Tree
17aedec- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
31e608e- Parent 2
90d48b3
6 files changed+179 added−73 removed
modified:examples/chat/app.js+1 added-4 removed
10 unmodified lines11121314151617181932 unmodified lines52535455555657585657585917 unmodified lines10 unmodified lines use(ContentLength) use(Cookie) use(Cache, { lifetime: (5).minutes, reapInterval: (1).minute }) use(Session, { lifetime: (15).minutes, reapInterval: (1).minute }) use(Static) use(Logger) set('root', __dirname)})
32 unmodified lines clearInterval(timer) }, 100)})
get('/public/*', function(file){ this.sendfile(__dirname + '/public/' + file)})
get('/*.css', function(file){ this.render(file + '.sass.css', { layout: false })})
17 unmodified linesadded:lib/express/plugins/static.js+101 added
1+2+// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3+4+/**5+ * Module dependencies.6+ */7+8+var path = require('path'),9+ fs = require('fs')10+ 11+// --- File12+13+exports.File = new Class({14+ 15+ /**16+ * Initialize with file _path_.17+ *18+ * @param {string} path19+ * @api public20+ */21+ 22+ constructor: function(path) {23+ this.path = path24+ if (path.indexOf('..') != -1)25+ Error.raise('InvalidPathError', "`" + path + "' is not a valid path")26+ },27+ 28+ /**29+ * Transfer static file to the given _request_.30+ *31+ * - Ensures the file exists32+ * - Ensures the file is a regular file (not FIFO, Socket, etc)33+ * - Automatically assigns content type34+ * - Halts with 404 when failing35+ *36+ * @param {Request} request37+ * @settings 'cache static files'38+ * @api public39+ */40+ 41+ sendTo: function(request) {42+ var cache, file = this.path43+ if (set('cache static files') && (cache = request.cache.get(file)))44+ return request.contentType(cache.type),45+ request.halt(200, cache.content, 'binary')46+ path.exists(file, function(exists){47+ if (!exists) return request.halt()48+ fs.stat(file, function(err, stats){49+ if (err) throw err50+ if (!stats.isFile()) return request.halt()51+ fs.readFile(file, 'binary', function(err, content){52+ if (err) throw err53+ request.contentType(file)54+ if (set('cache static files'))55+ request.cache.set(file, { type: file, content: content })56+ request.halt(200, content, 'binary')57+ })58+ })59+ })60+ }61+})62+63+// --- Static64+65+exports.Static = Plugin.extend({66+ extend: {67+ 68+ /**69+ * Initialize extensions.70+ */71+ 72+ init: function(options) {73+ options = options || {}74+ options.path = options.path || set('root') + '/public'75+ 76+ // Routes77+ 78+ get('/public/*', function(file){79+ this.sendfile(options.path + '/' + file)80+ })81+ 82+ // Request83+ 84+ Request.include({85+ 86+ /**87+ * Transfer static file at the given _path_.88+ *89+ * @param {string} path90+ * @return {Request}91+ * @api public92+ */93+94+ sendfile: function(path) {95+ (new exports.File(path)).sendTo(this)96+ return this97+ }98+ })99+ }100+ }101+})modified:lib/express/plugins.js+1 added
1234567893 unmodified lines
// Express - Plugins - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
global.merge(require('express/plugins/hooks'))global.merge(require('express/plugins/static'))global.merge(require('express/plugins/flash'))global.merge(require('express/plugins/cache'))global.merge(require('express/plugins/cookie'))global.merge(require('express/plugins/session'))3 unmodified linesmodified:spec/node.js+1 added-1 removed
19 unmodified lines2021222324242526273 unmodified lines3132333435363738399 unmodified lines19 unmodified lines 'routing', 'utils', 'request', 'mime', 'static', 'plugins', 'plugins.cache', 'plugins.view', 'plugins.content-length',3 unmodified lines 'plugins.hooks', 'plugins.cookie', 'plugins.session', 'plugins.flash', 'plugins.static', ]}
switch (process.ARGV[2]) {9 unmodified linesadded:spec/spec.plugins.static.js+75 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
describe 'Express' describe 'StaticFile' before StaticFile = require('express/plugins/static').File use(require('express/plugins/static').Static, { path: 'spec/fixtures' }) end describe '#constructor' it 'should accept and assign #path' (new StaticFile('/foo/bar')).path.should.eql '/foo/bar' end it 'should throw an InvalidPathError when .. is found' // TODO: use throw_error when fixed... try { new StaticFile('/../foobar') } catch (e) { e.name.should.eql 'InvalidPathError' e.message.should.eql "`/../foobar' is not a valid path" } end end describe 'GET /public/*' it 'should transfer static files' get('/public/user.json').body.should.include '"name":' end end describe '#sendfile()' describe 'when the file exists' it 'should transfer the file' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').body.should.include '"name":' end it 'should automatically set the content type based on extension' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' end end describe 'when "cache static files" is enabled' it 'should transfer the file' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/cached', function(){ return this.cache.get('spec/fixtures/user.json') ? 'yes' : 'no' }) get('/cached').body.should.eql 'no' get('/public/user.json').body.should.include '"name":' get('/cached').body.should.eql 'yes' get('/cached').body.should.eql 'yes' end it 'should automatically set the content type based on extension' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' end end end endendNo newline at end of filedeleted:spec/spec.static.js-68 removed
1-2-describe 'Express'3- describe 'StaticFile'4- before5- StaticFile = require('express/static').File6- end7- 8- describe '#constructor'9- it 'should accept and assign #path'10- (new StaticFile('/foo/bar')).path.should.eql '/foo/bar'11- end12- 13- it 'should throw an InvalidPathError when .. is found'14- // TODO: use throw_error when fixed...15- try { new StaticFile('/../foobar') }16- catch (e) {17- e.name.should.eql 'InvalidPathError'18- e.message.should.eql "`/../foobar' is not a valid path"19- }20- end21- end22- 23- describe '#sendfile()'24- describe 'when the file exists'25- it 'should transfer the file'26- get('/public/*', function(file){27- this.sendfile('spec/fixtures/' + file)28- })29- get('/public/user.json').body.should.include '"name":'30- end31- 32- it 'should automatically set the content type based on extension'33- get('/public/*', function(file){34- this.sendfile('spec/fixtures/' + file)35- })36- get('/public/user.json').headers['content-type'].should.eql 'application/json'37- end38- end39- 40- describe 'when "cache static files" is enabled'41- it 'should transfer the file'42- enable('cache static files')43- get('/public/*', function(file){44- this.sendfile('spec/fixtures/' + file)45- })46- get('/cached', function(){47- return this.cache.get('spec/fixtures/user.json') ? 'yes' : 'no'48- })49- get('/cached').body.should.eql 'no'50- get('/public/user.json').body.should.include '"name":'51- get('/cached').body.should.eql 'yes'52- get('/cached').body.should.eql 'yes'53- end54- 55- it 'should automatically set the content type based on extension'56- enable('cache static files')57- get('/public/*', function(file){58- this.sendfile('spec/fixtures/' + file)59- })60- get('/public/user.json').headers['content-type'].should.eql 'application/json'61- get('/public/user.json').headers['content-type'].should.eql 'application/json'62- get('/public/user.json').headers['content-type'].should.eql 'application/json'63- end64- end65- end66- 67- end68-end