-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
162 lines (102 loc) · 3.43 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// #!/bin/env node
// OpenShift Node application
//Get the environment variables we need.
var ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8000;
// Including the Node.js modules we need, and creating a http & web socket server
var express = require("express");
var expressApp = express();
var app = require("http").createServer(expressApp);
var io = require("socket.io/lib/socket.io").listen(app);
var fs = require("fs");
var url = require("url");
var mysql = require("mysql");
// Connecting to MySQl and selecting a database
var mysql_con = mysql.createConnection({
host : "localhost",
user : "root",
password : "",
database : "anonymouse"
});
expressApp.use('/static',express.static(__dirname+"/static"));
app.listen(port, ip_address);
console.log("Server listening on ",port,ip_address);
expressApp.get("/index.html",function(request,response){
handler(request,response);
});
// Routing http requests
expressApp.get("/index",function(request,response){
handler(request,response);
});
expressApp.get("/",function(request,response){
handler(request,response);
});
expressApp.get("/startup.html", function (request, response) {
handler(request, response);
});
expressApp.get("/startup", function (request, response) {
handler(request, response);
});
expressApp.get("/admin.html",function(request,response){
handler(request,response);
});
expressApp.get("/admin", function (request, response) {
handler(request, response);
});
expressApp.get("/*",function(request,response){
pageNotFound(request,response);
});
function handler(request,response){
var pathname = url.parse(request.url).pathname;
switch(pathname){
case "/index.html": route(pathname,response);
break;
case "/index" : route("/index.html",response);
break;
case "/" : route("/index.html",response);
break;
case "/startup.html" : route(pathname,response);
break;
case "/startup": route("/startup.html", response);
break;
case "/admin.html" : route(pathname,response);
break;
case "/admin": route("/admin.html", response);
break;
}
}
function route(pathname,response){
fs.readFile(__dirname+""+pathname,function(error,data){
if(error){
response.writeHead(404);
response.end("Page not found");
}else{
response.writeHead(200,{"Content-Type" : "text/html"});
response.write(data);
response.end();
}
});
}
function pageNotFound(request,response){
fs.readFile(__dirname + "/pagenotfound.html", function (error, data) {
if(error){
response.writeHead(404);
response.end("Page not found");
}else{
response.writeHead(200,{"Content-Type" : "text/html"});
response.write(data);
response.end();
}
});
}
// Listening to socket connection
io.sockets.on("connection",function(client){
require(__dirname+"/js/authenticator").authenticate(client,mysql_con,fs);
require(__dirname + "/js/chat").chat(client, mysql_con, fs);
require("./js/catcha_server").catcha_server(client, mysql_con, fs);
});
io.of("/backend").on("connection", function(client) {
require("./js/startup_tables").startup_tables(client, mysql_con, fs);
require("./js/startup_samples").startup_samples(client, mysql_con, fs);
require("./js/catcha_server").catcha_server(client, mysql_con, fs);
});