The JavaScript blog.


node modules npm caching

Node Roundup: npm-pkgr, Promised Land, Stash.js

Posted on .


npm-pkgr (GitHub: vvo / npm-pkgr, License: ISC, npm: npm-pkgr) by Vincent Voyer caches npm install results based on your package.json and npm-shrinkwrap.json files.

Depending on your set up, this should reduce the time taken to deploy Node projects. The basic usage is npm-pkgr instead of npm install, and npm-pkgr --production to use npm-shrinkwrap.json.

Promised Land

Promised Land (GitHub: FredyC / promised-land, License: MIT, npm: promised-land) provides a way of wrapping modules with an EventEmitter that allows you to use promises across your application.

For example, let's say you've got a module that connects to a database. Once the connection is ready, it does this:

var Land = require('promised-land');  
Land.emit('database:connected', db);  

Any other module that needs to know when the database is ready can now do so with promises:

var Land = require('promised-land');  
Land.promise('database:connected').then(function(db) {  

It also allows events to be repeated, in a stream-like way:

Land.stream('database:row').onValue(function(val) {  

The project has tests and more examples in the readme.


Stash.js (GitHub: tadeuzagallo / stash.js, License: MIT, npm: stash.js) by Tadeu Zagallo is a multilayer cache manager. That means you can define and use different storage systems for caching, based on "drivers".

So far the author has added a driver for ephemeral storage and localStorage support for browsers.

Cache items have methods for determining cache miss, locking, and setting values:

var stash = new Stash.Pool();  
var item = stash.getItem('my/key/path');  
var data = item.get();

if (item.isMiss()) {  
  data = 'example';
  item.set(data, cacheDuration);

What lock does here is dependent on the cache policy. There are four policies that are explained in the readme.


node express caching

The BMEAN Stack and Offline-First Design

Posted on .

This is a guest post written by Daishi Kato.

Many of you would know the MEAN stack, the software stack consisting of MongoDB, Express, AngularJS and Node. However, the BMEAN stack might be new to you. The B stands for Breeze, a data management library with support for client caching.

The current trend in web development is mobile first design. Recently, the "offline first" approach got attention for contributing toward better mobile user experience. Breeze allows you to develop offline web applications easily thanks to support for querying the client's cache.

Breeze provides a sample BMEAN application called Zza, but it's not an offline application. I thought there could be a simpler
but more practical application using social-cms-backend.

Social-cms-backend is Express middleware and it demonstrates how to develop a simple Twitter clone in a matter of minutes. I updated social-cms-backend so that it supports Breeze and you don't have to code the server side logic.

Using the new social-cms-backend and the BMEAN stack, I developed a sample application called notes-app-sample. It's an offline application with HTML5 cache manifest thanks to connect-cache-manifest.

The usage of the application is pretty straightforward: you write text which will be stored in localStorage. Once you login, you can synchronize data with MongoDB on the server. The login is handled by passport, and currently it works with the Facebook strategy.

The source code of notes-app-sample is available here and the working application is available here.

I hope these sample applications give you some ideas about how to use Breeze with your MEAN apps.