-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (116 loc) · 3.89 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
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
var http = require("http");
var Layer = require("./lib/layer");
var makeRoute = require("./lib/route");
var methods = require("methods");
var monkeyPatchRequest = require('./lib/request');
var monkeyPatchResponse = require('./lib/response');
module.exports = function() {
var index;
var request, response, next;
var app = function(req, res, parentNext) {
index = 0;
request = req;
response = res;
next = parentNext; // save parentNext
app.monkeyPatch(request, response);
app.next();
};
app.listen = function(port, done) {
var server = http.createServer(app);
server.listen(port, done);
return server;
};
app.stack = [];
app.use = function(path, middleware) {
if (typeof(path) === "function") {
middleware = path;
path = "/";
}
app.stack.push(new Layer(path, middleware));
};
app.handle = function(req, res, next) {
};
app.next = function(err) {
var layer = app.stack[index++];
if (layer === undefined) {
// now at the bottom of the middleware stack
if (next) {
// at the end of subapp, should return to the parent app by calling next
next(err);
} else {
response.statusCode = err? 500 : 404;
response.end();
return;
}
} else {
var m = layer.handle;
request.params = {};
var match = layer.match(request.url);
if (match) {
request.params = match['params'];
if (typeof m.handle === "function") {
// this middleware is a subapp
if (request.url.indexOf(match.path) === 0) { // strip prefix of parent layer
request.url = request.url.substring(match.path.length, request.url.length);
}
request.app = app._parentApp;
response.app = app._parentApp;
if (m.stack.length > 0){
m(request, response, app.next);
} else {
app.next(err);
}
}
try {
if (err) {
if (m.length >= 4) {
m(err, request, response, app.next);
} else {
app.next(err);
}
} else {
// without error
if (m.length < 4) {
// this middleware does not handle error
m(request, response, app.next);
} else {
app.next(err);
}
}
} catch(e) {
// should return 500 for uncaught error
app.next(e);
}
} else {
app.next(err);
}
}
};
methods.forEach(function(method) {
app[method] = function(path, middleware) {
var handler = makeRoute(method, middleware);
if (handler) {
app.use(path, handler);
}
};
});
app._parentApp = null;
app.monkeyPatch = function(req, res) {
if (!('isExpress' in req)) {
var oldReq = req.__proto__;
monkeyPatchRequest.__proto__ = oldReq;
req.__proto__ = monkeyPatchRequest;
}
app._parentApp = req.app;
req.app = app;
req.res = res;
if (!('isExpress' in res)) {
var oldRes = res.__proto__;
monkeyPatchResponse.__proto__ = oldRes;
res.__proto__ = monkeyPatchResponse;
}
res.app = app;
res.req = req;
};
return app;
};