-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.js
53 lines (44 loc) · 1.19 KB
/
contract.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
48
49
50
51
52
53
'use strict';
const inherits = require('util').inherits;
//--- ugly express router nature
const Router = function(opts) {
opts = opts || {};
this.params = {};
this._params = [];
this.caseSensitive = opts.caseSensitive;
this.mergeParams = opts.mergeParams;
this.strict = opts.strict;
this.stack = [];
};
Router.prototype = Object.create(require('express').Router().__proto__);
//---
const Contract = function(opts) {
Router.call(this, opts);
};
inherits(Contract, Router);
Contract.prototype.__init = function(units) {
this.units = units;
};
Contract.prototype.use = function(path, contract) {
if (!contract) {
contract = path;
path = '/';
}
if (contract.handle) {
Contract.super_.prototype.use.call(this, path, (req, res, next) => contract.handle(req, res, next));
} else {
Contract.super_.prototype.use.call(this, path, contract);
}
return this;
};
Contract.prototype.addView = function(path, viewName) {
const view = this.units.require(viewName);
this.get(path, (req, res) => view.get(req, res));
return this;
};
Contract.prototype.addViews = function(views) {
for (let path in views) {
this.addView(path, views[path]);
}
};
module.exports = Contract;