-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.js
executable file
·37 lines (33 loc) · 1016 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
34
35
36
37
/* router.js */
/* gr-webserver */
"use strict"
var fs = require('fs')
function route(handle, pathname, response, request)
{
// console.log('About to route a request for ' + pathname);
var _pos = pathname.indexOf (".htm");
if (_pos > 0) // page request
{
var _fn = "." + pathname;
// console.log ("Request for " + _fn);
fs.readFile (_fn, 'utf8', function (err,data)
{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data);
response.end();
});
}
else if (typeof handle[pathname] === 'function')
{
return handle[pathname](response, request);
}
else
{
console.log ('No request handler found for ' + pathname);
response.writeHead(404 ,{'Content-Type': 'text/plain'});
response.write ('404 Not Found');
response.write (pathname + "\n");
response.end();
}
}
exports.route = route;