forked from ODZ-UJF-AV-CR/Web-FIK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAPP.js
71 lines (60 loc) · 2.16 KB
/
GAPP.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 app = express();
const port = 3000;
const fs = require('fs');
app.set('view engine', 'ejs');
app.set('views', 'views');
// Route to send JSON data
app.get('/api/data', (req, res) => {
try {
const rawData = fs.readFileSync('data.json', 'utf8');
const rawData2 = fs.readFileSync('dataLocation.json', 'utf8');
const decodedData = JSON.parse(rawData);
const decodedData2 = JSON.parse(rawData2);
console.log(decodedData);
// Send JSON data as a response
res.json({ data: decodedData});
} catch (error) {
res.status(500).json({ error: 'Error loading data' });
}
});
// Route to render HTML
app.get('/', async (req, res) => {
try {
const rawData = fs.readFileSync('data.json', 'utf8');
const rawData2 = fs.readFileSync('dataLocation.json', 'utf8');
const decodedData = JSON.parse(rawData);
const decodedData2 = JSON.parse(rawData2);
// Render the HTML template and include JSON data
res.render('index', { data: decodedData});
} catch (error) {
res.status(500).send('Error rendering the page');
}
});
app.get('/api/get/data', async (req, res) => {
try {
const rawData = fs.readFileSync('data.json', 'utf8');
const rawData2 = fs.readFileSync('dataLocation.json', 'utf8');
const decodedData = JSON.parse(rawData);
const decodedData2 = JSON.parse(rawData2);
// Render the HTML template and include JSON data
res.json({ data: decodedData});
} catch (error) {
res.status(500).send('Error rendering the page');
}
});
// Route to receive JSON data via POST request
app.post('/api/get/data', (req, res) => {
try {
// Assuming you're expecting JSON data in the request body
const receivedData = req.body;
console.log(receivedData);
// Process the received data as needed
res.status(200).json({ message: 'Data received successfully' });
} catch (error) {
res.status(500).json({ error: 'Error processing data' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});