0.3.1 • Published 8 years ago

speedy-static v0.3.1

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

speedy-static

Serve your static files with an high-speed middleware

NPM version Downloads Build Status Coverage Status

speedy-static is an high-speed middleware that allows you to serve files statically. It provides some useful options in order to make itself as near as possible to your needs.

ChangeLog

  • 0.3.1 - download and downloadRegExp options implemented
  • 0.3.0 - Auto-compilation of ".less", ".coffee", ".cson" and ".yml"
  • 0.2.3 - Index option implemented
  • 0.1.7 - Hide dotfiles option implemented
  • 0.1.6 - numeraljs format supported by max-cache-size option
  • 0.1.4 - JSON minimization
  • 0.1.3 - Cache-Control header, s-maxage implemented

How it works

speedy-static is fully optimized to reach the best performance in serving static resources.

The server side optimizations:

  • use a LRU cache in order to limit as more as possible I/O waits generated by filesystem calls
  • compress and minimize to reduce up to 70% the size of your source files
  • stores already compiled, compressed and minimized source files into LRU to save CPU and memory
  • can prepare cache to make the middleware start with the most of resources already loaded in memory
  • allow the programmer to ignore some resources or an entire tree in order to prevent useless filesystem calls or wasting of cache space

The client side optimizations:

  • use conditional headers to prevent the server send multiple times the same resources payload
  • prevent the browser to frequently ask the server to validate resources using a validation time

How to install

    npm install speedy-static

How to use it

    var express = require("express");
    var app = express();

    var speedyStatic = require("speedy-static");

    // if you don't want to prepare cache it returns the middleware
    var middleware = speedyStatic("/path/to/mount");
    app.use("/mount/point", middleware);

    // if you want to prepare cache before having the middleware,
    // it will return you a promise in order to give you the middleware
    // as soon as the cache is prepared
    speedyStatic("/path/to/mount", {"prepare-cache":true})
                .then(function(middleware){
                    app.use("/mount/point", middleware);
                });

API

speedyStatic(pathToMount, options);

The path to mount is the root path of the static files you want to serve. The options allows you to make your tuning.

Options

NameDescriptionDefault
indexIt allows you to define your index files (it works for all the directories)"index.html", "index.htm"
compile(1)It can make auto-compilations from ".less" to ".css", from ".coffee" to ".js" and from ".cson" and ".yml" to ".json"".less", ".coffee", ".cson", ".yml"
compressionIt allows you to serve files compressing them when possibletrue
compression-levelIt defines the level of the compression 0=BEST_SPEED,1=DEFAULT_COMRESSION,2=BEST_COMPRESSION1
minifyIt allows you to minimize source files when possibile .js,.css,.jsonfalse
minify-mangleIt allows you to also mangle minimized source filestrue
etagIt produces and sends to the client the ETag header in order to validate further requests of the same resourcetrue
last-modifiedIt reads the last modification date and sends to the client the Last-Modified header in order to validate further requests of the same resourcetrue
content-typeIt writes the Content-Type header referred to the requested resourcetrue
max-cache-sizeIt defines the size limit (in bytes) of the LRU cache (it supports numeraljs formats)104857600 (100MB)
max-cache-ageIt defines the expiration time (in milliseconds) of the LRU cache elements0 (never expire)
prepare-cacheIt prepares the LRU cache before giving the middlewarefalse
browser-cacheIt allows you to use the browser cache to optimize the amount of calls and datatrue
browser-cache-max-ageIt defines the expiration time (in seconds) of the browser cache resources300
browser-cache-s-maxageIt allows you to override the intermediary (such as CDNs) max-age and expires headers300
hide-dotfilesIt allows you to hide dotfilestrue
ignoreIt allows you to ignore resources. It works with an entire path or a single resource name
ignoreRegExpIt allows you to ignore resources defining regular expressions. It works only on file names
download(2)It allows you to make resources downloadable. It works with an entire path or a single resource name
downloadRegExp(2)It allows you to make resources downloadable defining regular expressions. It works only on file names
continueIt allows you to pass the request to the next middleware instead of ending itfalse
ignore-errorsIt allows you to ignore server errors hiding them with a 404false
  1. Compiled files can be minimized, mangled and compressed as if they were been fetched already compiled.
  2. Downloadable resources won't be compiled, minimized or compressed.

Unit testing

To test speedy-static be sure that mocha and istanbul were installed, otherwise you can install them typing

    npm install -g mocha
    npm install -g istanbul

Be also sure that devDependencies was installed, otherwise you can install them jumping into speedy-static directory and typing

    npm install

Then, inside speedy-static directory type the following command.

    npm test

Licence

Licenced under MIT Copyright (c) 2016 - Pietro Cucinella