node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


Data Cache Layer

Your routes shouldn't need to know where data is coming from. You should be able to swap out data sources in a relatively easy manner and still have your websites templates render the exact same way. You should be able to connect the same data to different output formats with ease.

This module provides a way to access your data through internal routes and provides the ability to cache the data. It also provides a template rendering controller and JSON rendering controller.


Add a handler for the specified url.

  • route -- internal url
  • handler -- function to grab the requested data
  • ttl -- cache time to live

hander -> function(req, callback)

  • req -- is the request object. Currently only url and params are set.
  • callback -- function to call when the data is retrieved


addRoute('blog/:slug', function(req, cb) {
  getBlogPost(req.params.slug, cb)


Helper to easily compile several routes into one object

  • route -- internal url
  • dependencies -- key value pairs. Key -> dependent url, Value -> key to put data in the returned object.


  { 'blog/:slug': 'blogPost'
  , 'blog/:slug/comments': 'comments'
// Generates 
// { blogPost: blogPostData 
// , comments: commentData 
// } 


Get data from specified route.

  • route -- the url to grab
  • callback -- callback to call with err or data when data is retrieved.


getData('blog/first-post', function(err, post) {
  if (err) return handleErr(err)


External url handler that will render a template with the data result from an internal url

  • dataRoute -- Internal route to get data from
  • template -- Template to render


app.get('/blog/:slug', connectDataTemplate('blog/:slug/data', 'blogPost'))


External url handler that gives JSON output of internal url data

  • dataRoute -- Internal route to get data from


app.get('/api/blog/:slug/comments', connectJSONTemplate('blog/:slug/comments'))