Skip to content
Myr

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

6 files+179 added−73 removed

modified:examples/chat/app.js+1 added-4 removed
added:lib/express/plugins/static.js+101 added
12// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */78var path = require('path'),9 fs = require('fs')10 11// --- File1213exports.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})6263// --- Static6465exports.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 */9394 sendfile: function(path) {95 (new exports.File(path)).sendTo(this)96 return this97 }98 })99 }100 }101})
modified:lib/express/plugins.js+1 added
modified:spec/node.js+1 added-1 removed
added:spec/spec.plugins.static.js+75 added
deleted:spec/spec.static.js-68 removed
12describe '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 end68end