-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (49 loc) · 1.4 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
// express stuff
var url = require('url'),
http = require('http'),
endpoints = require("./endpoints.js"),
routing = require("./routing.js");
function express(){
var middlewareStore = [];
function app(request, response){
app.handle(request, response, routing(request));
}
app.route = function(path, fn){
app[path] = app[path] || {};
app[path].store = app[path].store || [];
app[path].store.push(fn);
};
app.use = function (fn){
middlewareStore.push(fn);
};
app.handle = function (request, response, path){
var index = 0;
var that = app[path] && app[path].store ? app[path] : app.generic;
var store = middlewareStore;
function runner(err) {
var lay = store[index];
if (!lay){
if (store === middlewareStore){
index = 0;
store = this.store;
next();
}
return;
}
index++;
if (err && lay.length < 4) {
next(err);
} else if (err){
lay(err, request, response, next);
} else if (lay.length > 3) {
next();
} else {
lay(request, response, next);
}
}
next = runner.bind(that);
next();
};
return app;
}
module.exports = express;