A Connect middleware for mapping routes to memory
Want to see pretty graphs? Log in now!
npm install connect-file-cache
|262||downloads in the last day|
|1 676||downloads in the last week|
|7 893||downloads in the last month|
|Last Published By|
|Version||0.2.5 last updated a year ago|
If you're just serving files, Connect's own static (possibly with staticCache on top) is quite sufficient. But suppose you want to serve, say, compiled and minified CoffeeScript with a far-future expires header, and you want multiple routes to be able to point at that same file (say, one with an MD5 suffix for disambiguation, and one without).
That's the use case connect-file-cache was born out of. It's designed to be very fast. The "files" it serves don't have to exist on disk at all. On the other hand, it's not suitable for serving large files (or many), because all files are stored in memory.
Bonus: connect-file-cache also does gzip (when requested)!
How do I use it?
npm install connect-file-cache
Then in your app:
connectCache = require 'connect-file-cache' cache = connectCache options app.use cache.middleware
options is a hash that may include
src: A dir containing files to be served directly (defaults to
routePrefix: Data will be served from this path (defaults to
You can map routes directly to strings like so:
cache.set 'foo/bar.txt', 'str'
Then go to
yourapp/foo/bar.txt in your browser.
You can map multiple routes by passing in an array as the first argument:
cache.set ['foo/bar.txt', 'foo/baz.txt'], 'str'
And if you want the browser to download the file rather than trying to display it, set the
attachment flag to
cache.set 'foo/bar.txt', 'str', attachment: true
By default, MIME types are automatically inferred from file extensions. But you can override this with the
cache.set 'foo/bar.txt', 'str', mime: 'html'
To support browser-side caching, connect-file-cache handles "If-Modified-Since" requests. But to prevent even those few milliseconds of extra latency, you can set
false—meaning that the browser will never download the file again (from the same route):
cache.set 'foo/bar.txt', 'str', expires: false
For more advanced examples, check the
Thoroughly inspired by Connect's static middleware.
©2011 Trevor Burnham and available under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.