1 | 'use strict';
|
2 |
|
3 | const baiji = require('../');
|
4 | const Controller = baiji.Controller;
|
5 | const express = require('express');
|
6 | const debug = require('debug')('baiji:examples:express');
|
7 |
|
8 |
|
9 | process.on('uncaughtException', function(e) {
|
10 | debug('uncaughtException', e, e.stack);
|
11 | });
|
12 |
|
13 |
|
14 | let ArticlesCtrl = baiji('articles');
|
15 |
|
16 | ArticlesCtrl.before('index', function(ctx, next) {
|
17 | debug('before index executed.');
|
18 | setTimeout(next, 200);
|
19 | });
|
20 |
|
21 | ArticlesCtrl.before('*', function(ctx, next) {
|
22 | debug('before * executed.');
|
23 | next();
|
24 | });
|
25 |
|
26 | ArticlesCtrl.after('index', function(ctx, next) {
|
27 | debug('after index executed.');
|
28 | next();
|
29 | });
|
30 |
|
31 | ArticlesCtrl.define('index', {
|
32 | description: 'fetch article list',
|
33 | accepts: [
|
34 | { name: 'q', type: 'string', description: 'keyword used for searching articles' },
|
35 | { name: 'ids', type: ['number'], description: 'article ids' }
|
36 | ],
|
37 | route: { verb: 'get', path: '/' }
|
38 | }, function(ctx, next) {
|
39 | debug('method executed', ctx.methodName);
|
40 | ctx.done(ctx.args);
|
41 | next();
|
42 | });
|
43 |
|
44 | ArticlesCtrl.define('show', {
|
45 | description: 'fetch article detail',
|
46 | accepts: [
|
47 | { name: 'id', type: 'number', description: 'article id' }
|
48 | ],
|
49 | route: { verb: 'get', path: '/:id' }
|
50 | }, function(ctx, next) {
|
51 | debug('method executed', ctx.methodName);
|
52 | ctx.done({
|
53 | id: ctx.args.id,
|
54 | title: 'baiji usage post',
|
55 | content: 'see readme.'
|
56 | });
|
57 | next();
|
58 | });
|
59 |
|
60 | class UsersCtrl extends Controller {
|
61 | constructor() {
|
62 | super();
|
63 | this.setName('users');
|
64 |
|
65 |
|
66 | this.beforeAction('loginRequired', { except: 'index' });
|
67 | this.beforeAction('checkAppExistance');
|
68 | this.beforeAction(function customBeforeAction(ctx, next) {
|
69 | debug('custom beforeAction called');
|
70 | next();
|
71 | }, { only: ['show'] });
|
72 | }
|
73 |
|
74 | initConfig() {
|
75 | return {
|
76 | index: { description: 'user list', route: { path: '/', verb: 'get' } },
|
77 | show: { description: 'user detail', route: { path: '/:id', verb: 'get' } }
|
78 | };
|
79 | }
|
80 |
|
81 | loginRequired(ctx, next) {
|
82 | debug('loginRequired executed');
|
83 | next();
|
84 | }
|
85 |
|
86 | checkAppExistance(ctx, next) {
|
87 | debug('checkAppExistance executed');
|
88 | next();
|
89 | }
|
90 |
|
91 | index(ctx, next) {
|
92 | debug('method executed', ctx.methodName);
|
93 | ctx.done([
|
94 | { id: 1, username: 'felix' },
|
95 | { id: 2, username: 'jenny' }
|
96 | ]);
|
97 | next();
|
98 | }
|
99 |
|
100 | show(ctx, next) {
|
101 | debug('method executed', ctx.methodName);
|
102 | this.handleApp();
|
103 | ctx.done({
|
104 | id: 1,
|
105 | username: 'felix'
|
106 | });
|
107 | next();
|
108 | }
|
109 |
|
110 | handleApp() {
|
111 | debug('UsersCtrl.prototype.handleApp called');
|
112 | }
|
113 | }
|
114 |
|
115 |
|
116 | let app = baiji('myApp');
|
117 |
|
118 |
|
119 | app.set('adapterOptions.arrayItemDelimiters', ',');
|
120 |
|
121 |
|
122 | app.enable('x-powered-by');
|
123 |
|
124 |
|
125 | app.define('404', {
|
126 | description: 'handle 404',
|
127 | route: { verb: 'all', path: '*' }
|
128 | }, function(ctx, next) {
|
129 | debug('method executed', ctx.methodName);
|
130 | ctx.done({
|
131 | error: {
|
132 | name: '404',
|
133 | message: `no url available for ${ctx.method.toUpperCase()} ${ctx.path}`
|
134 | }
|
135 | });
|
136 | next();
|
137 | });
|
138 |
|
139 | app.before('*', function(ctx, next) {
|
140 | debug('before all executed.');
|
141 | next();
|
142 | });
|
143 |
|
144 | app.after('*', function(ctx, next) {
|
145 | debug('after all executed.');
|
146 | next();
|
147 | });
|
148 |
|
149 | app.afterError('*', function(ctx, next) {
|
150 | debug('afterError * executed.');
|
151 | debug('afterError =>', ctx.error, ctx.error.stack);
|
152 | ctx.done({ error: { name: ctx.error, stack: ctx.error.stack } });
|
153 | next();
|
154 | });
|
155 |
|
156 |
|
157 | app.use(ArticlesCtrl, { mountPath: '/articles' });
|
158 |
|
159 |
|
160 | app.use(UsersCtrl, { mountPath: '/users' });
|
161 |
|
162 |
|
163 | let subApp = express();
|
164 |
|
165 | subApp.get('/info', function(req, res) {
|
166 | res.send('express app info');
|
167 | });
|
168 |
|
169 |
|
170 | app.use(app, { mountPath: '/app' });
|
171 |
|
172 |
|
173 | app.use(subApp, {
|
174 | name: 'subApp',
|
175 | desc: 'express App',
|
176 | mountPath: 'subApp',
|
177 | skipHooks: false
|
178 | });
|
179 |
|
180 |
|
181 | app.plugin('evaluator');
|
182 |
|
183 | debug('app is listening on port 3005');
|
184 | app.listen(3005);
|