-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (35 loc) · 1.39 KB
/
server.js
File metadata and controls
43 lines (35 loc) · 1.39 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
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
import machineRoutes from './routes/machineRoutes.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = process.env.PORT || 3001;
app.use(cors());
app.use(express.json());
// Servir les fichiers statiques (images, CSS, assets buildés)
// Tout est maintenant regroupé dans 'public'
app.use(express.static(path.join(__dirname, 'public')));
// Routes API
app.use('/api/machines', machineRoutes);
// Page d'accueil / SPA
app.get('/', (req, res) => {
if (process.env.NODE_ENV === 'production') {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
} else {
res.send('<h1>Backend ClarifAI actif</h1><p>Accédez au frontend sur <a href="http://localhost:8080">http://localhost:8080</a> en mode dev.</p><p><a href="/api/machines">Voir les machines (/api/machines)</a></p>');
}
});
// Pour toutes les autres routes non-API, renvoyer l'index de l'app (mode SPA)
if (process.env.NODE_ENV === 'production') {
app.get('*', (req, res, next) => {
if (req.path.startsWith('/api')) return next();
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
}
app.listen(port, "0.0.0.0", () => {
console.log(`Backend is listening on http://0.0.0.0:${port}`);
});