Added static file support [#17]
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent c5491c9 · Browse files · 4 files·+77 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+77 added−11 removed
- tests:+19 added−0 removed
- other:+58 added−11 removed
Provenance
- Commit
542a90a- Tree
97626a8- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
c5491c9
4 files changed+77 added−11 removed
modified:examples/app.js+2 added-11 removed
10 unmodified lines111213141516151617181923 unmodified lines4344454648495051525354555647484950513 unmodified lines10 unmodified lines enable('cache views')})
var messages = [], path = require('path'), posix = require('posix') StaticFile = require('express/static').File get('/', function(){ this.redirect('/chat')})23 unmodified lines }, 100)})
get('/public/:file', function(file){ var self = this file = dirname(__filename) + '/public/' + file path.exists(file, function(exists){ if (!exists) self.halt() posix.cat(file).addCallback(function(content){ self.contentType(file) self.halt(200, content) }) }) (new StaticFile(dirname(__filename) + '/public/' + file)).send(this)})
get('/error', function(){ throw new Error('oh noes!')3 unmodified linesadded:lib/express/static.js+56 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
var path = require('path'), posix = require('posix') // --- InvalidPathError
InvalidPathError = ExpressError.extend({ name: 'InvalidPathError', init: function(path) { this.message = "`" + path + "' is not a valid path" }}) // --- File
exports.File = Class({ /** * Initialize with file _path_. * * @param {string} path * @api public */ init: function(path) { this.path = path if (path.indexOf('..') != -1) throw new InvalidPathError(path) }, /** * Transfer static file to the given _request_. * * - Ensures the file exists * - Ensures the file is a regular file (not FIFO, Socket, etc) * - Automatically assigns content type * * @param {Request} request * @api public */ send: function(request) { var file = this.path path.exists(file, function(exists){ if (!exists) request.halt() posix.stat(file).addCallback(function(stats){ if (!stats.isFile()) request.halt() posix.cat(file).addCallback(function(content){ request.contentType(file) request.halt(200, content) }) }) }) }})No newline at end of filemodified:spec/spec.node.js+1 added
25 unmodified lines2627282930313233349 unmodified lines25 unmodified lines .exec('spec/spec.routing.js') .exec('spec/spec.helpers.js') .exec('spec/spec.request.js') .exec('spec/spec.mime.js') .exec('spec/spec.static.js') .exec('spec/spec.collection.js') .exec('spec/spec.element-collection.js') .exec('spec/spec.plugins.js') .exec('spec/spec.plugins.view.js')9 unmodified linesadded:spec/spec.static.js+18 added
123456789101112131415161718
describe 'Express' describe 'StaticFile' before StaticFile = require('express/static').File end describe '#init' it 'should accept and assign #path' (new StaticFile('/foo/bar')).path.should.eql '/foo/bar' end it 'should throw an InvalidPathError when .. is found' -{ new StaticFile('/../foobar') }.should.throw_error(/InvalidPathError/) end end endendNo newline at end of file