-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
209 lines (186 loc) · 4.69 KB
/
app.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const http = require("http")
const express = require('express')
const session = require('express-session') // session module
const sharedsession = require("express-socket.io-session")
const app = express()
const server = http.createServer(app)
const io = require('socket.io').listen(server)
const bodyparser = require("body-parser")
const fs = require('fs')
const ejs = require('ejs')
const path = require('path')
// var connection = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "",
// database: "atp12a"
// });
app.use(bodyparser.urlencoded({ 'extended': 'true' })) // you should write this
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/css'))
app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist'))
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.use(session({
'secret': 'session',
'resave': 'true',
'saveUninitialized': 'true'
}))
/*
// use with mysql
var pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'root',
password: '',
database: 'atp12a',
debug: false
});
*/
username = null
// app.get('*', function (req, res) {
// res.render('maintenance')
// })
app.get('/', function (req, res) { // home redirects
if (req.session.username) {
res.redirect('/chat')
}
else {
res.redirect('/login')
}
if (req.session.loginstatus == "false") { // wrong login
// console.log('req.session.loginstatus: false')
res.write('username or password wrong');
req.session.loginstatus = "";
res.end()
}
})
app.route('/login')
.get((req, res) => { // if /submit is sent with get
if (req.session.username) {
res.redirect('/chat')
}
else {
res.render('index')
}
})
.post((req, res) => {
if (req.session.username) {
res.redirect('/chat')
}
else if (req.body.username) {
req.session.username = req.body.username;
req.session.userpass = req.body.userpass;
username = req.session.username
res.redirect('/chat')
}
else {
res.render('index', { message: 'Bir isim yazmalısın.' })
}
/*
// use with mysql
var sql = "SELECT * FROM `admins` WHERE `adminname` = '" + `${username}` + "' AND `adminpass` = '" + `${userpass}` + "'";
connection.query(sql, function (err, result, fields) {
try {
var row = result.length;
// console.log('row:'+row)
} catch (error) {
// console.log('catch: -' + error)
}
if (row > 0) {
if (req.body) {
req.session.username = username;
req.session.userpass = userpass;
myvar = req.session.username; //
req.session.loginstatus = "true";
// console.log('login-status: 1')
res.redirect('/chat')
res.end()
} else {
req.session.loginstatus = "false";
// console.log('login-status: 0')
res.redirect('/')
}
}
})
*/
})
app.get('/chat', function (req, res) {
if (req.session.username) {
username = req.session.username
res.render('chat', { username })
}
else {
res.redirect('/login')
}
})
// /logout
app.get('/logout', function (req, res) {
delete req.session.username
username = null
// req.session.destroy() // deletes all sessions
delete req.session.userpass
// console.log('session deleted')
res.redirect('/')
})
var connectedguys = new Array();
/* This is auto initiated event when Client connects to Your Machien. */
io.on('connection', function (socket) {
console.log(username + ' is connected');
socket.on('me', function (status) {
var text = status.text;
var user = username;
// var user = status.user;
if (username) {
io.emit('refresh feed', { text, user });
}
else {
io.emit('not logged');
}
/*
// use with mysql
add_status({ text, user }, function (res) {
if (res) {
io.emit('refresh feed', { text, user });
} else {
io.emit('error');
}
});
*/
});
socket.on('disconnect', function () { // when user disconnect
console.log(username + ' is disconnected');
})
});
app.use(function (req, res) {
res.render('404')
})
const port = process.env.PORT || 80;
if (port == null || port == "") {
port = 80;
}
server.listen(port);
console.log('Server working at http://localhost:' + port)
/*
// use with mysql
var add_status = function (status, callback) {
pool.getConnection(function (err, connection) {
if (err) {
callback(false);
return;
}
var sql = "INSERT INTO `fbstatus` (`s_user`, `s_text`) VALUES ('" + `${myvar}` + "','" + `${status.text}` + "')";
// var sql = "Select* from admins";
connection.query(sql, function (err, result, rows) {
connection.release();
// console.log(result)
if (!err) {
callback(true);
}
});
connection.on('error', function (err) {
callback(false);
return;
});
});
}
*/