Skip to content
Myr

Static is now a plugin

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 31e608e · Browse files · 5 files+161 added−69 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+161 added−69 removed

  • tests:+70 added−69 removed
  • other:+91 added−0 removed

5 files+161 added−69 removed

added:lib/express/plugins/static.js+90 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() {73 Request.include({74 75 /**76 * Transfer static file at the given _path_.77 *78 * @param {string} path79 * @return {Request}80 * @api public81 */8283 sendfile: function(path) {84 (new exports.File(path)).sendTo(this)85 return this86 }87 })88 }89 }90})
modified:lib/express/plugins.js+1 added
modified:spec/node.js+1 added-1 removed
added:spec/spec.plugins.static.js+69 added
deleted:spec/spec.static.js-68 removed