-
Notifications
You must be signed in to change notification settings - Fork 0
/
genIndex.js
115 lines (98 loc) · 3.38 KB
/
genIndex.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
var express = require('express');
var fs = require('fs');
var mysql = require('mysql');
var Mustache = require('./public/js/mustache.js');
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
server.listen(8888);
var connection = mysql.createConnection({
host: '',
user: '',
password: '',
database: ''
});
app.use(express.bodyParser());
app.get('/', function(req, res) {
res.sendfile('./index.html');
console.log("Request receieved: " + req.route);
});
app.get('/static', function(req, res) {
console.log("File Requested: " + req.param('file'));
if(req.param('file').indexOf('.css') + 1)
res.sendfile('./public/css/' + req.param('file'));
else if(req.param('file').indexOf('.js') + 1)
res.sendfile('./public/js/' + req.param('file'));
});
app.get('/check', function(req, res) {
var uname = req.param('username');
console.log(uname);
var data = {username: uname};
connection.query('SELECT * FROM user where username = ?', data['username'], function(err, rows) {
if(err) throw err;
console.log(rows[0]);
var yon;
if(rows[0] == undefined)
yon = 0;
else
yon = 1;
console.log("Request received to check username");
console.log("Username found?" + yon);
res.json({yn: yon});
});
});
app.get('/welcome', function(req, res) {
var name = fs.readFileSync("./welcome.html");
var data = {username: req.param('username')};
var html = Mustache.render(String(name), data);
res.send(html);
});
app.post('/createUser', function(req, res) {
var data = {username: req.param('username')};
connection.query('INSERT INTO user SET ?', data);
connection.query('SELECT * from rooms', function(err, rows) {
if(err) throw err;
if(rows[0]!=undefined) {
roomName = [];
rows.forEach(function(row) {
roomName.push(row['roomname']);
});
data = {username: data['username'], rooms: roomName};
}
else
data = {username: data['username'], rooms: ""};
console.log("this is received: " + data);
});
res.redirect('/welcome?username=' + data['username']);
});
app.post('/createRoom', function(req, res) {
console.log('Roomname ' + req.param('cRoom') + '\n Username ' + req.param('username'));
var data = {roomname: req.param('cRoom'), username: req.param('username')};
connection.query('INSERT INTO rooms SET?', data);
console.log('1st insert');
connection.query('INSERT INTO ' + data['roomname'] + ' SET?', udata);
console.log('2nd insert');
res.redirect('/joinChat?username=' + udata['username'] + '&roomname=' + data['roomname'] + '&create=0');
});
app.get('/joinChat', function(req, res) {
var data = {roomname: req.param('roomname'), username: req.param('username')};
if(req.param('create') == 1) {
connection.query('INSERT INTO ' + data['roomname'] + ' SET?', data['username']);
console.log('3rd insert');
}
var file = fs.readFileSync('./converse.html');
var html = Mustache.to_html(String(file), data);
//startChat();
res.send(html);
});
app.get('/exit', function(req, res) {
var room = {roomname: req.param('roomname')};
connection.query('DELETE FROM rooms where roomname=?', room);
connection.query('SELECT count(username) from ' + room['roomname'], function(err, rows) {
if(err) throw err;
if(rows[0] == 0)
connection.query('DROP TABLE ' + room['roomname']);
});
res.sendfile('./logout');
});