-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (97 loc) · 2.74 KB
/
Copy pathserver.js
File metadata and controls
112 lines (97 loc) · 2.74 KB
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
// Import the express module
const express = require('express');
const fs = require('fs');
const cors = require('cors');
const bodyParser = require('body-parser');
// Create an instance of an Express application
const app = express();
// Define a port to listen on
const PORT = process.env.PORT || 3018;
// Middleware
app.use(cors()); // Enable CORS for all routes
app.use(bodyParser.json()); // Parse JSON request bodies
// In-memory array to store saved objects
let objects = [];
let eduUsers = {};
// Define a simple route
app.get('/', (req, res) => {
fs.readFile('./Home/Console_en.html', 'utf8', (err, data) => {
if (err) {
res.send(err);
} else {
res.send(data);
}
})
});
// POST route to save an object
app.post('/save-object', (req, res) => {
const newObject = req.body; // Get the object from the request body
objects.push(newObject); // Save the object to the in-memory array
res.status(201).send({ message: 'Object saved successfully!', object: newObject });
});
// GET route to retrieve saved objects (for testing)
app.get('/objects', (req, res) => {
res.json(objects);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
app.get('/core', (req, res) => {
fs.readFile('./core/Home_core.html', 'utf8', (err, data) => {
if (err) {
res.send(err);
} else {
res.send(data);
}
})
});
app.get('/core/home', (req, res) => {
fs.readFile('./core/Home_core.html', 'utf8', (err, data) => {
if (err) {
res.send(err);
} else {
res.send(data);
}
})
});
app.get('/edu', (req, res) => {
fs.readFile('./edu/Home_edu.html', 'utf8', (err, data) => {
if (err) {
res.send(err);
console.error("Error:" + err);
} else {
res.send(data);
}
})
});
app.post('/save-user', (req, res) => {
const userInfo = req.body; // Get the object from the request body
fs.writeFile('./edu/users/Users.json', 'utf8', (err, data) => {});
fs.appendFile('./edu/users/Users.json', '{' + userInfo + '}', (err) => {
if (err) {
console.error(err);
return;
}
console.log('Content has been appended');
});
});
app.get('/users', (req, res) => {
fs.readFile('./edu/users/Users.json', 'utf8', (err, data) => {
if (err) {
res.send(err);
} else {
res.json(data);
}
});
});
app.get('/edu/openplay', (req, res) => {
fs.readFile('./edu/Play_edu.html', 'utf8', (err, data) => {
if (err) {
res.send(err);
console.error("Error:" + err);
} else {
res.send(data);
}
})
});