UNPKG

2.04 kBJavaScriptView Raw
1'use strict';
2
3const baiji = require('../');
4const debug = require('debug')('baiji:examples:socketio');
5
6let app = baiji('myApp');
7app.set('adapter', 'socketio');
8
9app.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
17let ArticlesCtrl = baiji('articles');
18
19ArticlesCtrl.before('index', function(ctx, next) {
20 debug('before index executed.');
21 setTimeout(next, 200);
22});
23
24process.on('uncaughtException', function(e) {
25 debug('uncaughtException', e, e.stack);
26});
27
28ArticlesCtrl.before('*', function(ctx, next) {
29 debug('before * executed.');
30 next();
31});
32
33ArticlesCtrl.after('index', function(ctx, next) {
34 debug('after index executed.');
35 next();
36});
37
38ArticlesCtrl.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
51ArticlesCtrl.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
66app.before('*', function(ctx, next) {
67 debug('before all executed.');
68 next();
69});
70
71app.after('*', function(ctx, next) {
72 debug('after all executed.');
73 next();
74});
75
76app.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
83app.use(ArticlesCtrl);
84
85app.listen(3006);
86debug('app is listening on port 3006');