-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
34 lines (27 loc) · 846 Bytes
/
router.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
var reqHandlers = require('./requestHandlers');
/*
var handlers = {};
handlers['/'] = reqHandlers.start;
handlers['/start'] = reqHandlers.start;
handlers['/upload'] = reqHandlers.upload;
*/
function getHandler(pathname) {
if (pathname == '/') {
return reqHandlers.start;
} else {
var action = pathname.substr(1);
return reqHandlers[action];
}
}
function route(pathname, req, rsp) {
console.log('routing request for '+pathname);
var handler = getHandler(pathname);
if (typeof handler === 'function') {
handler(req, rsp);
} else {
console.log('no request handler found for '+pathname);
var responseHelper = require('./responseHelper');
responseHelper.respond(404, 'text/plain', 'no request handler found for '+pathname, rsp);
}
}
exports.route = route;