-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstack.js
91 lines (73 loc) · 2.14 KB
/
stack.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
module.exports = Stack;
var Url = require('url');
function Stack(/*layers*/) {
var error = Stack.errorHandler,
handle = error;
Array.prototype.slice.call(arguments).reverse().forEach(function (layer) {
var child = handle;
handle = function (req, res) {
try {
layer(req, res, function (err) {
if (err) { return error(req, res, err); }
child(req, res);
});
} catch (err) {
error(req, res, err);
}
};
});
return handle;
}
Stack.errorHandler = function error(req, res, err) {
if (err) {
console.error(err.stack);
res.writeHead(500, {"Content-Type": "text/plain"});
res.end(err.stack + "\n");
return;
}
res.writeHead(404, {"Content-Type": "text/plain"});
res.end("Not Found\n");
};
function core(req, res, next) { next(); }
// Build a composite stack made of several layers
Stack.compose = function compose(/*layers*/) {
// Don't bother composing singletons
if (arguments.length == 1) { return arguments[0]; }
var stack = core;
Array.prototype.slice.call(arguments).reverse().forEach(function (layer) {
var child = stack;
stack = function (req, res, next) {
try {
layer(req, res, function (err) {
if (err) { return next(err); }
child(req, res, next);
});
} catch (err) {
next(err);
}
};
});
return stack;
}
// Mounts a substack app at a url subtree
Stack.mount = function mount(mountpoint/*, stack*/) {
var stack = Stack.compose.apply(null, Array.prototype.slice.call(arguments, 1));
if (mountpoint.substr(mountpoint.length - 1) == "/") {
mountpoint = mountpoint.substr(0, mountpoint.length - 1);
}
var matchpoint = mountpoint + "/";
return function (req, res, next) {
var url = req.url;
var uri = req.uri;
if (url.substr(0, matchpoint.length) !== matchpoint) { return next(); }
// Modify the url
if (!req.realUrl) { req.realUrl = url; }
req.url = url.substr(mountpoint.length);
if (req.uri) { req.uri = Url.parse(req.url); }
stack(req, res, function (err) {
req.url = url;
req.uri = uri;
next(err)
});
};
};