-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
47 lines (40 loc) · 1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
const path = require('path');
const Router = require('./lib/router');
module.exports = app => {
const router = new Router(app);
// proxy app.router
const rootRouter = router.namespace();
Object.defineProperty(app, 'router', {
get() {
return rootRouter;
},
});
/**
* get sub router
*
* @function Router#namespace
* @param {String} prefix - sub router prefix
* @param {...Function} [middlewares] - optional middlewares
* @return {Router} Return sub router with special prefix
*/
app.router.namespace = (...args) => {
return router.namespace(...args);
};
/**
* get url by name
*/
app.url = router.url.bind(router);
// patch loadRouter
// load sub routers first
const loadRouter = app.loader.loadRouter;
app.loader.loadRouter = function() {
new app.loader.FileLoader({
directory: path.join(app.baseDir, 'app/router'),
target: {},
inject: app,
call: true,
}).load();
loadRouter.call(app.loader);
};
};