Skip to content

Commit 606f827

Browse files
committed
JS: add tests for various route handler registration patterns
1 parent 9c50acc commit 606f827

File tree

3 files changed

+910
-0
lines changed

3 files changed

+910
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
var express = require("express");
2+
var app = express();
3+
4+
// registration of route handlers in bulk
5+
let routes0 = {
6+
a: (req, res) => console.log(req),
7+
b: (req, res) => console.log(req)
8+
};
9+
for (const p in routes0) {
10+
app.get(p, routes0[p]);
11+
}
12+
13+
// registration of route handlers in bulk
14+
let routes1 = {
15+
a: (req, res) => console.log(req),
16+
b: (req, res) => console.log(req)
17+
};
18+
for (const handler of routes1) {
19+
app.use(handler);
20+
}
21+
22+
// registration of route handlers in bulk, with indirection
23+
let routes2 = {
24+
a: (req, res) => console.log(req),
25+
b: (req, res) => console.log(req)
26+
};
27+
for (const p of Object.keys(routes2)) {
28+
app.get(p, routes2[p]);
29+
}
30+
31+
// registration of route handlers in bulk, with indirection
32+
let routes3 = {
33+
a: (req, res) => console.log(req),
34+
b: (req, res) => console.log(req)
35+
};
36+
for (const h of Object.values(routes3)) {
37+
app.use(h);
38+
}
39+
40+
// custom router indirection for all requests
41+
let myRouter1 = {
42+
handlers: {},
43+
add: function(n, h) {
44+
this.handlers[n] = h;
45+
},
46+
handle: function(req, res, target) {
47+
this.handlers[target](req, res);
48+
}
49+
};
50+
myRouter1.add("whatever", (req, res) => console.log(req));
51+
app.use((req, res) => myRouter1.handle(req, res, "whatever"));
52+
53+
// simpler custom router indirection for all requests
54+
let mySimpleRouter = {
55+
handler: undefined,
56+
add: function(h) {
57+
this.handler = h;
58+
},
59+
handle: function(req, res) {
60+
this.handler(req, res);
61+
}
62+
};
63+
mySimpleRouter.add((req, res) => console.log(req));
64+
app.use((req, res) => mySimpleRouter.handle(req, res));
65+
66+
// simplest custom router indirection for all requests
67+
let mySimplestRouter = {
68+
handler: (req, res) => console.log(req),
69+
handle: function(req, res) {
70+
this.handler(req, res);
71+
}
72+
};
73+
app.use((req, res) => mySimplestRouter.handle(req, res));
74+
75+
// a combination of bulk registration and indirection through a custom router
76+
let myRouter3 = {
77+
handlers: {},
78+
add: function(n, h) {
79+
this.handlers[n] = h;
80+
},
81+
handle: function(req, res, target) {
82+
this.handlers[target](req, res);
83+
}
84+
};
85+
let routes3 = {
86+
a: (req, res) => console.log(req),
87+
b: (req, res) => console.log(req)
88+
};
89+
for (const p of Object.keys(routes3)) {
90+
myRouter3.add(p, routes3[p]);
91+
}
92+
app.use((req, res) => myRouter3.handle(req, res, "whatever"));
93+
94+
// a combination of bulk registration and indirection through a custom router. Using a map instead of an object.
95+
let myRouter4 = {
96+
handlers: new Map(),
97+
add: function(n, h) {
98+
this.handlers.set(n, h);
99+
},
100+
handle: function(req, res, target) {
101+
this.handlers.get(target)(req, res);
102+
}
103+
};
104+
let routes4 = {
105+
a: (req, res) => console.log(req),
106+
b: (req, res) => console.log(req)
107+
};
108+
for (const p of Object.keys(routes4)) {
109+
myRouter4.add(p, routes4[p]);
110+
}
111+
app.use((req, res) => myRouter4.handle(req, res, "whatever"));
112+
113+
// registration of imported route handlers in bulk
114+
let importedRoutes = require("./route-collection").routes;
115+
for (const p in importedRoutes) {
116+
app.get(p, importedRoutes[p]);
117+
}
118+
app.get("a", importedRoutes.a);
119+
app.get("b", importedRoutes.b);
120+
121+
// registration of imported route handlers in a map
122+
let routesMap = new Map();
123+
routesMap.set("a", (req, res) => console.log(req));
124+
routesMap.set("b", (req, res) => console.log(req));
125+
routesMap.forEach((v, k) => app.get(k, v));
126+
app.get("a", routesMap.get("a"));
127+
app.get("b", routesMap.get("a"));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.routes = {
2+
a: (req, res) => console.log(req),
3+
b: (req, res) => console.log(req)
4+
};

0 commit comments

Comments
 (0)