-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtransformerRouter.js
More file actions
43 lines (36 loc) · 1.38 KB
/
transformerRouter.js
File metadata and controls
43 lines (36 loc) · 1.38 KB
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
function route(pathname, req, res, body) {
console.log(
"transformerRouter:route() About to route a request for " + pathname
);
try {
// dynamically load the js file base on the url path
var handler = require("." + pathname);
var respToReturn = "Failed to process request";
console.log("transformerRouter:route() selected handler: " + handler);
// make sure we got a correct instantiation of the module
if (typeof handler.post === "function") {
// route to the right method in the module based on the HTTP action
if (req.method.toLowerCase() == "get") {
respToReturn = handler.get(req, res, body);
} else if (req.method.toLowerCase() == "post") {
respToReturn = handler.post(req, res, body);
} else if (req.method.toLowerCase() == "put") {
respToReturn = handler.put(req, res, body);
} else if (req.method.toLowerCase() == "delete") {
respToReturn = handler.delete(req, res, body);
}
console.log("transformerRouter:route() routed successfully");
return respToReturn;
}
} catch (err) {
console.log(
"transformerRouter:route() exception instantiating handler: " + err.stack
);
throw err;
}
var errorMessage =
"transformerRoute:route() No request handler found for " + pathname;
console.log(errorMessage);
throw new Error(errorMessage);
}
exports.route = route;