1 | /**
|
2 | * Example Waif service.
|
3 | *
|
4 | * Waif services are standard express apps, but instead
|
5 | * of listening to a connection on their own, they export
|
6 | * the app object.
|
7 | *
|
8 | * These apps are going to be mounted in express, but
|
9 | * they can actually be anything that conforms to the
|
10 | * behavior of connect middleware.
|
11 | *
|
12 | * You can probably get away with just Express.Router
|
13 | * most of the time.
|
14 | */
|
15 |
|
16 | // Get the waif instance.
|
17 | var waif = require('waif')();
|
18 |
|
19 | // Normal Express app.
|
20 | var app = require('express')();
|
21 |
|
22 | // Services are simple request wrappers.
|
23 | var store = waif('store');
|
24 | var postList = waif('postList');
|
25 |
|
26 | // The REST is all up to you.
|
27 | app.get('/post/:id', function(req, res, next) {
|
28 | store('/posts/' + req.param.id, _showBody('post');
|
29 | });
|
30 |
|
31 | app.get('/', function(req, res, next) {
|
32 | dataList('/', _showBody('list'));
|
33 | });
|
34 |
|
35 | // export app instead of listening on a port
|
36 | module.exports = app;
|
37 |
|
38 | //// HELPERS
|
39 |
|
40 | function _showBody(template) {
|
41 | return function(err, data) {
|
42 | if (err) { return next(err); }
|
43 | res.render('post', { body: data.body });
|
44 | };
|
45 | }
|