-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (83 loc) · 2.6 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
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'binuschat'
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to database:', err);
} else {
console.log('Connected to database');
}
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('Binusall'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/Binusall/public/loginbinuschat.html');
});
app.post('/login', (req, res) => {
const { email, password } = req.body;
console.log(email, password);
const query = `SELECT * FROM useraccount WHERE (Email = '${email}' OR NIM = '${email}') AND Pass = '${password}'`;
connection.query(query, (error, results) => {
if (error) {
console.error('Error executing query:', error);
res.sendStatus(500);
return;
}
if (results.length > 0) {
const { Nama, NIM, imagepath, Email} = results[0];
res.redirect(`http://localhost:3000/chatweb?Nama=${Nama}&NIM=${NIM}&imagepath=${imagepath}&Email=${Email}`);
} else {
res.redirect('http://localhost:3000/');
}
});
});
app.get('/chatweb', (req, res) => {
res.sendFile(__dirname + '/Binusall/public/chatweb.html');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
app.get('/search', (req, res) => {
const searchText = req.query.searchText;
const query = `SELECT * FROM useraccount WHERE NIM LIKE '${searchText}' OR Email LIKE '${searchText}'`;
connection.query(query, (error, results) => {
if (error) {
console.error('Error executing query:', error);
res.sendStatus(500);
return;
}
res.json(results);
});
});
app.post('/send-message', (req, res) => {
const { message, senderId, receiverId } = req.body;
const query = `INSERT INTO messages (messagesend, senderid, receiverid) VALUES ('${message}', '${senderId}', '${receiverId}')`;
connection.query(query, (error, results) => {
if (error) {
console.error('Error executing query:', error);
res.sendStatus(500);
return;
}
res.json({ message: 'Message sent successfully' });
});
});
app.get('/messages', (req, res) => {
const query = 'SELECT * FROM messages';
connection.query(query, (error, results) => {
if (error) {
console.error('Error executing query:', error);
res.sendStatus(500);
return;
}
res.json(results);
});
});