-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (35 loc) · 923 Bytes
/
index.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
var finalhandler = require('finalhandler');
module.exports = (app, path, enable=true) => {
if (!enable) {
var initApp = require(path);
initApp(app);
return;
}
app.handle = function handle(req, res, callback) {
var router = this._router;
// final handler
var done = callback || finalhandler(req, res, {
env: this.get('env'),
onerror: logerror.bind(this)
});
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
delete this._router;
this.lazyrouter();
var initApp = require(path);
initApp(this);
router = this._router;
// no routes
if (!router) {
debug('no routes defined on app');
done();
return;
}
router.handle(req, res, done);
};
}
function logerror(err) {
/* istanbul ignore next */
if (this.get('env') !== 'test') console.error(err.stack || err.toString());
}