busterc / save-static

:anchor: Saves Express responses to a static directory for disk caching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

:anchor: Saves Express responses to a static directory for disk caching

License:ISC License


Languages

Language:JavaScript 99.8%Language:HTML 0.2%