-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (120 loc) · 4.18 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
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
const express = require('express');
const mysql = require('mysql');
const http = require('http');
const WebSocket = require('ws');
require('dotenv').config();
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const fs = require('fs');
const path = require('path');
const cors = require('cors');
app.use(cors())
// Create MySQL connection
const connection = mysql.createConnection({
host: process.env.HOST_NAME,
user: process.env.HOST_USER,
password: process.env.HOST_PASSWORD,
database: process.env.DATABASE
});
// Connect to MySQL
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ', err);
return;
}
console.log('Connected to MySQL database...');
});
// WebSocket server
wss.on('connection', (ws) => {
console.log('Client connected');
// Send initial data to client upon connection
connection.query('SELECT * FROM flight', (error, results) => {
if (error) {
console.error('Error fetching initial data: ', error);
return;
}
ws.send(JSON.stringify(results));
});
// Listen for close event
ws.on('close', () => {
console.log('Client disconnected');
});
});
app.get('/api/admin', (req, res) => {
// Read the content of admin.json file
fs.readFile('./admin-data/admin.json', 'utf8', (err, data) => {
if (err) {
console.error('Error reading admin.json file:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
try {
const admins = JSON.parse(data).admins;
res.json(admins);
} catch (error) {
console.error('Error parsing JSON data:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
})
app.get('/api/:id', (req, res) => {
const id = req.params.id;
// SQL query with parameter for id
const sql = `SELECT * FROM flight WHERE id = ?`;
// Execute the query with a prepared statement (prevents SQL injection)
connection.query(sql, [id], (err, results) => {
if (err) {
console.error('Error retrieving data:', err);
return res.status(500).send('Internal Server Error');
}
if (results.length === 0) {
return res.status(404).send('Not Found');
}
// Send the retrieved data in JSON format
res.json(results[0]);
});
});
app.patch('/api/update/:id/:etd/:gate/:remark/:delay', (req, res) => {
const ID = req.params.id;
const ETD = req.params.etd;
const GATE = req.params.gate;
const REMARK = req.params.remark;
const DELAY = req.params.delay;
console.log(ETD, GATE, REMARK, DELAY, ID);
// SQL query with parameters for id, time, destination, and delay
const sql = `UPDATE flight SET ETD = ?, GATE = ?, REMARK = ?, DELAY = ? WHERE ID = ?`;
// Execute the query with a prepared statement
connection.query(sql, [ETD, GATE, REMARK, DELAY, ID], (err, results) => {
if (err) {
console.error('Error updating data:', err);
return res.status(500).send('Internal Server Error');
}
console.log(results.affectedRows + " record(s) updated");
// Send a success message
res.json({ message: 'Data updated successfully' });
});
});
// app.post('/api/addnew/:flightid/:std/:etd/:logo/:destination/:gate', (res, res) => {
// const STD = req.params.std;
// const ETD = req.params.etd;
// const LOGO = req.params.logo;
// const DESTINATION = req.params.destination;
// const GATE = req.params.gate;
// const ID = req.params.flightid;
// const sql = `INSERT INTO flight (STD, ETD, LOGO, ID, DESTINATION, GATE) VALUE (?, ?, ?, ?, ?, ?)`;
// connection.query(sql, [STD, ETD, LOGO, ID, DESTINATION, GATE], (err, results) =>{
// if (err) {
// console.error('Error inserting data:', err);
// res.status(500).json({ error: 'Error inserting data' });
// return;
// }
// console.log('Data inserted successfully');
// res.status(200).json({ message: 'Data inserted successfully' });
// })
// })
// Start server
const PORT = 8080;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});