⚓️ Saves Express responses to a static directory for disk caching
JavaScript HTML
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
gulpfile.js
package.json

README.md

save-static NPM version Build Status Dependency Status

Save Express responses to a static directory for disk caching

Use save-static as a callback on Express Application.render() or Response.render() and it will:

  • Write the rendered response body (HTML) to a file on disk
    • after sending the response to the client (to avoid slow responses)

Then subsequent requests can be served from disk using:

Use in conjunction with serve-static-x for caching and re-caching files to disk.

Install

$ npm install --save save-static

Usage

// be sure to require it
var SaveStatic = require('save-static');

// initialize with the root path for saving to disk
var staticPath = __dirname + '/static'; // <= for example
var saveStatic = new SaveStatic(staticPath);

// ...

// here's a sample Express route
app.get('/nfl/afc/east/teams', function(req, res, next) {

  res.render('division', {
    teams: ['Patriots', 'Jets', 'Bills', 'Dolphins']
  }, saveStatic(res)); // <= easy as that

  // the render HTML will be saved to a file on disk at:
  // staticPath + /nfl/afc/east/teams.html

  // NOTE: you can pass a callback too, e.g. saveStatic(res, next)
  // however, if there is no error, the response will have already
  // been sent to the client.

  // NOTE: a route for "/" will be saved on disk as 'index.html'

  // NOTE: a route for "/something.xhtml" will be saved 
  // on disk as 'something.xhtml'

};

License

ISC © Buster Collings