1 | 'use strict';
|
2 |
|
3 | const baiji = require('../');
|
4 | const debug = require('debug')('baiji:examples:socketio');
|
5 |
|
6 | let app = baiji('myApp');
|
7 | app.set('adapter', 'socketio');
|
8 |
|
9 | app.define('*', {
|
10 | description: 'handle unknown method'
|
11 | }, function(ctx, next) {
|
12 | debug('method executed', ctx.methodName);
|
13 | ctx.done({ error: { name: 'no method error', message: `no method called ${ctx.clientMethodName}` } });
|
14 | next();
|
15 | });
|
16 |
|
17 | let ArticlesCtrl = baiji('articles');
|
18 |
|
19 | ArticlesCtrl.before('index', function(ctx, next) {
|
20 | debug('before index executed.');
|
21 | setTimeout(next, 200);
|
22 | });
|
23 |
|
24 | process.on('uncaughtException', function(e) {
|
25 | debug('uncaughtException', e, e.stack);
|
26 | });
|
27 |
|
28 | ArticlesCtrl.before('*', function(ctx, next) {
|
29 | debug('before * executed.');
|
30 | next();
|
31 | });
|
32 |
|
33 | ArticlesCtrl.after('index', function(ctx, next) {
|
34 | debug('after index executed.');
|
35 | next();
|
36 | });
|
37 |
|
38 | ArticlesCtrl.define('index', {
|
39 | description: 'fetch article list',
|
40 | accepts: [
|
41 | { arg: 'q', type: 'string', description: 'keyword used for searching articles' },
|
42 | { arg: 'ids', type: ['number'], description: 'article ids' }
|
43 | ],
|
44 | http: { verb: 'get', path: '/' }
|
45 | }, function(ctx, next) {
|
46 | debug('method executed', ctx.methodName);
|
47 | ctx.done(ctx.args);
|
48 | next();
|
49 | });
|
50 |
|
51 | ArticlesCtrl.define('show', {
|
52 | description: 'fetch article detail',
|
53 | accepts: [
|
54 | { arg: 'id', type: 'number', description: 'article id' }
|
55 | ],
|
56 | http: { verb: 'get', path: '/:id' }
|
57 | }, function(ctx, next) {
|
58 | debug('method executed', ctx.methodName);
|
59 | ctx.done({
|
60 | title: 'baiji usage post',
|
61 | content: 'see readme.'
|
62 | });
|
63 | next();
|
64 | });
|
65 |
|
66 | app.before('*', function(ctx, next) {
|
67 | debug('before all executed.');
|
68 | next();
|
69 | });
|
70 |
|
71 | app.after('*', function(ctx, next) {
|
72 | debug('after all executed.');
|
73 | next();
|
74 | });
|
75 |
|
76 | app.afterError('*', function(ctx, next) {
|
77 | debug('afterError * executed.');
|
78 | debug('afterError =>', ctx.error, ctx.error.stack);
|
79 | ctx.done({ error: { name: ctx.error, stack: ctx.error.stack } });
|
80 | next();
|
81 | });
|
82 |
|
83 | app.use(ArticlesCtrl);
|
84 |
|
85 | app.listen(3006);
|
86 | debug('app is listening on port 3006');
|