-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
33 lines (28 loc) · 1004 Bytes
/
App.js
File metadata and controls
33 lines (28 loc) · 1004 Bytes
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
const express = require('express');
const path = require('path');
const mysql = require('mysql2/promise');
const dbConfig = require('./config/database');
const app = express();
const port = 3000;
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'view.html'));
});
app.get('/api/data', async (req, res) => {
const connection = await mysql.createConnection(dbConfig);
try {
const [students] = await connection.execute('SELECT * FROM student');
const [lines] = await connection.execute('SELECT * FROM line');
res.json({ students, lines });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Error fetching data' });
} finally {
await connection.end();
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
// node app.js (터미널에서 이 코드로 실행)
//http://127.0.0.1:3000