web-pockets

Build loosely-coupled web-apps without caring about ordering.


License
BSD-2-Clause
Install
npm install web-pockets@0.3.1

Documentation

web-pockets Build Status

Build loosely-coupled web-apps without caring about ordering.

Table of Contents

  1. The Synopsis below is intended to give a taste of what web-pockets is.
  2. The best place to start learning more is the guide.
  3. There are reference docs for all the built-in values.
  4. For those wondering "why bother?" there is Why web-pockets is better than other frameworks.
  5. Some example code using web-pockets:

Synopsis

Create a handler function named app:

var app = require('./')();

app is also a pocket:

app.value('hits', createHitCounter);

function createHitCounter () {
  return new Promise(function (resolve) {
    // Pretend we did something more interesting here
    var hits = {};
    setTimeout(resolve, 1000, hits);
  });
}

You can also define values that are computed per-request:

app.request.value('acceptsJson', requestAcceptsJSON);
function requestAcceptsJSON (request) {
  return /json/.test(request.headers.accept);
}

Use app.route to get going quickly:

app.route('GET *', function (request, hits, acceptsJSON) {
  var count = hits[request.url] = (hits[request.url] || 0) + 1;
  if (acceptsJSON) {
    return { hits: count, url: request.url };
  }
  var message = request.url + ' has been visited ' + count + 'time';
  if (count !== 1) {
    message += 's';
  }
  return message;
});

API

Handler = Pocket & ((Request, Response) => void) & {
  request: PocketProxy
}

module.exports =: (Pocket?) => Handler

The default export takes an optional "root" pocket and returns a handler function that is also a pocket. The handler function has a property named request that is a deferred proxy for the child pocket implicitly created for each request. What this means is that values that should be computed once for the entire app are defined on the handler using e.g. app.value(name, valOrFunction), and values that should be computed once per-request are defined using app.request.value(name, valOrFunction).

See also: API docs for Pocket

License

MIT