-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateApp.js
128 lines (101 loc) · 2.67 KB
/
createApp.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var http = require('http');
var flattern = require('./utils/flattern');
var createRoute = require('./createRoute');
var App = Object.create(Function.prototype);
App.init = function () {
this.settings = Object.create(null);
this.routes = [];
};
App.set = function (key, val) {
this.settings[key] = val;
return this;
};
App.enable = function (key) {
this.settings[key] = true;
return this;
};
App.disable = function (key) {
this.settings[key] = false;
return this;
};
// conflict
// App.get = function (key, val) {
// return this.settings[key];
// };
App.enabled = function (key) {
return !!this.settings[key];
};
App.disabled = function (key) {
return !this.settings[key];
};
App.finalize = function (err, req, res) {
if(err) {
res.status = err.status || 500;
res.end(err.stack || err.toString());
} else {
res.status = 404;
return res.end('Can not ' + req.method + ' ' + req.url);
}
};
App.handle = function (req, res, next) {
var self = this;
var err;
next = next || function (err) {
var finalize = self.finalize;
finalize(err, req, res);
};
// TODO
// if(!next) next = function (err) { return finalize(err, req, res)}
// next = nextWithRestore(next, {})
return (function loop(i) {
var route = self.routes[i];
if(!route) return next(err);
return route(err, req, res, function (suberr) {
err = suberr;
return loop(i + 1);
});
}(0));
};
App.addRoute = function (method, path, pathOpts, fns) {
var route = createRoute(method, path, pathOpts);
fns.forEach(function (fn) {
route.addLayer(fn);
});
this.routes.push(route);
return this;
};
App.use = function(path /*, fn1, fn2 */) {
var fns;
// TODO pathOpts
var pathOpts = {};
if(typeof path === 'string') {
fns = flattern([].slice.call(arguments, 1));
return this.addRoute(null, path, pathOpts, fns);
} else {
fns = flattern([].slice.call(arguments, 0));
return this.addRoute(null, null, pathOpts, fns);
}
};
// TODO all
var methods = ['get', 'post'];
methods.forEach(function (method) {
App[method] = function (path /*, fn1, fn2, ... */) {
if(method === 'get' && arguments.length === 1) return this.settings[path];
var fns = flattern([].slice.call(arguments, 1));
// TODO pathOpts
var pathOpts = {};
return this.addRoute(method.toUpperCase(), path, pathOpts, fns);
};
});
App.listen = function () {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
module.exports = function createApp () {
var app = function (req, res, next) {
return app.handle(req, res, next);
};
app.__proto__ = App;
app.init();
return app;
};