web-pockets
v0.3.1
Published
Build loosely-coupled web-apps without caring about ordering.
Downloads
43
Readme
web-pockets
Build loosely-coupled web-apps without caring about ordering.
Table of Contents
- The Synopsis below is intended to give a taste of what
web-pockets
is. - The best place to start learning more is the guide.
- There are reference docs for all the built-in values.
- For those wondering "why bother?" there is Why
web-pockets
is better than other frameworks. - Some example code using web-pockets:
- An example of per-request values lives in this repo.
- feedr is a server that retrieves and normalizes RSS feeds.
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