-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (59 loc) · 1.81 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
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const cors = require('cors');
const app = express();
const port = 3000;
// Enable CORS for all routes
app.use(cors());
// Replace these values with your MySQL credentials
const dbConfig = {
//host: 'localhost',
host: 'sql12.freemysqlhosting.net',
//user: 'root',
user: 'sql12661489',
//password: 'mysql',
password: 'a6v1d7maPW',
//database: 'school'
database: 'sql12661489'
};
const connection = mysql.createConnection(dbConfig);
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL');
});
app.use(bodyParser.json());
app.use(express.static(__dirname));
app.get('/getStudents', (req, res) => {
const className = req.query.class || 'Class A';
const query = `SELECT * FROM school WHERE class = ? ORDER BY sort_rank`;
connection.query(query, [className], (err, results) => {
if (err) {
console.error('Error executing MySQL query:', err);
res.status(500).send('Internal Server Error');
return;
}
res.json(results);
});
});
app.post('/updateSortOrder', (req, res) => {
const updatedOrder = req.body.sortOrder;
const className = req.body.className || 'Class A';
updatedOrder.forEach((student, index) => {
const query = 'UPDATE school SET sort_rank = ? WHERE class = ? AND name_of_student = ?';
connection.query(query, [index + 1, className, student], (err) => {
if (err) {
console.error('Error updating MySQL record:', err);
res.status(500).send('Internal Server Error');
return;
}
});
});
res.send('Sort order updated successfully');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});