-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (72 loc) · 3.56 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/************************************************************
* INDEX.js *
*----------------------------------------------------------*
* AUTHOR: Christopher Webster *
* DATE: Oct. 3rd 2012 *
*----------------------------------------------------------*
* PURPOSE: *
* This is the first file to run. *
* The purpose of this code is to: *
* a)register initial routes *
* b)launch the server *
* c)expose basic functions for adding and checking routes *
* *
*----------------------------------------------------------*
* NOTES: *
* none *
* *
*----------------------------------------------------------*
* CHANGES: *
* DATE: *
* CHANGE: *
* **********************************************************/
var server = require("./server"),
router = require("./router"),
requestHandlers = require("./requestHandlers"),
util = require("util");
//State Variable for tracking initialization
var intialized = false;
var routes = {};
/************************************************************
* Register routes here *
* use "isHandle" to check if route is already registered *
* otherwise add it to the list of handles. *
* **********************************************************/
(function registerInitialRoutes(){
//Served with wrappers created by request handlers.
if(!isRoute("/")) {
addToRoutes("/", requestHandlers.main);
}
//Static Files.
if(!isRoute("/staticTest.html")) {
addToRoutes("/staticTest.html", function(response){requestHandlers.staticFile("./static_pages/staticTest.html","text/html",response);});
}
if(!isRoute("/styles/site.css")) {
addToRoutes("/styles/site.css", function(response){requestHandlers.staticFile("./styles/site.css","text/css",response);});
}
if(!isRoute("/scripts/main.js")) {
addToRoutes("/scripts/main.js", function(response){requestHandlers.staticFile("./scripts/main.js","text/javascript",response);});
}
})();
/************************************************************
* Start the Server *
************************************************************/
server.start(router.route, routes);
intialized = true;
/************************************************************
* Route Management Functions *
* This are provided and exported - so that routes can be *
* checked and registered from elsewhere *
************************************************************/
function addToRoutes(key, funcRoute)
{
routes[key] = funcRoute;
util.log("added " + key + " as route");
}
function isRoute(pathToCheck)
{
util.log("check path:" + pathToCheck);
return(typeof routes[pathToCheck] === 'function');
}
exports.isRoutes = isRoute;
exports.addToRoutes = addToRoutes;