-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrouter.js
More file actions
33 lines (29 loc) · 876 Bytes
/
router.js
File metadata and controls
33 lines (29 loc) · 876 Bytes
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 static = require('node-static');
var webroot = "./static";
var file = new(static.Server)(webroot, {
cache: 600,
headers: { 'X-Powered-By': 'node-static' }
});
// route incoming requests
function routeHttp(handler, path, request, response) {
//console.log("routing for: " + path);
// if there is a function for that path, call it
if (typeof handler[path] === 'function') {
handler[path](request, response);
}
else {
// otherwise try to serve a file
file.serve(request, response, function(err, result) {
if (err) {
console.error('Error serving %s - %s', request.url, err.message);
response.writeHead(err.status, err.headers);
response.write("404 not found");
response.end();
}
else {
console.log('%s - %s', request.url, response.message);
}
});
}
}
exports.routeHttp = routeHttp;