UNPKG

4.11 kBJavaScriptView Raw
1'use strict';
2
3const baiji = require('../');
4const Controller = baiji.Controller;
5const express = require('express');
6const debug = require('debug')('baiji:examples:express');
7
8// Handle all uncaughtException avoid node instance crashing
9process.on('uncaughtException', function(e) {
10 debug('uncaughtException', e, e.stack);
11});
12
13// Article Controller
14let ArticlesCtrl = baiji('articles');
15
16ArticlesCtrl.before('index', function(ctx, next) {
17 debug('before index executed.');
18 setTimeout(next, 200);
19});
20
21ArticlesCtrl.before('*', function(ctx, next) {
22 debug('before * executed.');
23 next();
24});
25
26ArticlesCtrl.after('index', function(ctx, next) {
27 debug('after index executed.');
28 next();
29});
30
31ArticlesCtrl.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
44ArticlesCtrl.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
60class UsersCtrl extends Controller {
61 constructor() {
62 super();
63 this.setName('users');
64
65 // Add hooks
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// Main app
116let app = baiji('myApp');
117
118// Allow string to be splited as array by specific delimiters
119app.set('adapterOptions.arrayItemDelimiters', ',');
120
121// Enable X-Powered-By
122app.enable('x-powered-by');
123
124// Handle all undefined routes
125app.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
139app.before('*', function(ctx, next) {
140 debug('before all executed.');
141 next();
142});
143
144app.after('*', function(ctx, next) {
145 debug('after all executed.');
146 next();
147});
148
149app.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// Mount Article Controller
157app.use(ArticlesCtrl, { mountPath: '/articles' });
158
159// Mount User Controller
160app.use(UsersCtrl, { mountPath: '/users' });
161
162// Init a new express app
163let subApp = express();
164
165subApp.get('/info', function(req, res) {
166 res.send('express app info');
167});
168
169// nested sub app
170app.use(app, { mountPath: '/app' });
171
172// Mount express app
173app.use(subApp, {
174 name: 'subApp',
175 desc: 'express App',
176 mountPath: 'subApp',
177 skipHooks: false
178});
179
180// Enable evaluator plugin
181app.plugin('evaluator');
182
183debug('app is listening on port 3005');
184app.listen(3005);