-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (30 loc) · 1022 Bytes
/
server.js
File metadata and controls
36 lines (30 loc) · 1022 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
34
35
36
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import { createServer as createViteServer } from 'vite';
const app = express();
const port = process.env.PORT || 3000;
// Obtenir le chemin du fichier actuel
const __filename = fileURLToPath(import.meta.url);
// Obtenir le répertoire du fichier actuel
const __dirname = path.dirname(__filename);
async function startServer() {
if (process.env.NODE_ENV === 'development') {
const vite = await createViteServer({
server: { middlewareMode: 'html' },
appType: 'custom',
});
app.use(vite.middlewares);
} else {
// Utiliser Vite pour le build en production
app.use(express.static(path.join(__dirname, 'dist')));
}
// Pour servir le fichier index.html en mode production
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
}
startServer();