36 lines
918 B
JavaScript
36 lines
918 B
JavaScript
import express from 'express';
|
|
import fetch from 'node-fetch';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
// Biar bisa akses __dirname di ES module
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// Endpoint home
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'public/index.html'));
|
|
});
|
|
|
|
// API Proxy: fetch data dari backend
|
|
app.get('/siswa', async (req, res) => {
|
|
try {
|
|
const response = await fetch('http://localhost:4000/data');
|
|
const data = await response.json();
|
|
res.json(data);
|
|
} catch (err) {
|
|
console.error('Gagal fetch API:', err);
|
|
res.status(500).json({ error: 'Gagal ambil data siswa' });
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log('Frontend running on port', PORT);
|
|
});
|
|
|
|
// Index.js frontend
|