-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (38 loc) · 1.26 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
const express = require('express');
const path = require('path');
const app = express();
const PORT = 3000;
const mysql = require('mysql');
// MySQL database configuration
const dbConfig = {
host: 'localhost',
user: 'newuser',
password: 'newpassword',
database: 'my_Db'
};
const connection = mysql.createConnection(dbConfig);
// Endpoint to fetch data and return as JSON
app.get('/data', (req, res) => {
const query = 'SELECT timestamp_column as label, value FROM my_Table LIMIT 10'; // Adjust the query as needed
connection.query(query, (err, results) => {
if (err) {
console.error('Database query error:', err);
res.status(500).json({ error: 'Database query error' });
return;
}
const labels = results.map(row => row.label);
const data = results.map(row => row.value);
res.json({labels,data});
});
});
app.get('/', (req, res) => {
// Send the HTML file as a response
res.sendFile(path.join(__dirname, 'chart.html'));
});
app.get('/chartdata', (req, res) => {
// Send the HTML file as a response
res.sendFile(path.join(__dirname, 'chartdata.txt'));
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});