Build loosely-coupled web-apps without caring about ordering.
- 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.
- feedrapp is a drop-in replacement for the deprecated Google Feed API that retrieves and normalizes RSS feeds.
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;
});
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
MIT