-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
72 lines (58 loc) · 1.79 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import path from 'path';
import authRoutes from './api/auth.js';
import favoritesRoutes from './api/favorites.js';
import foldersRoutes from './api/folders.js';
import seriesFavoritesRouter from './api/seriesFavorites.js';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors({
origin: ['https://webtech404.vercel.app', 'http://localhost:5173']
}));
// API-Routen verwenden
app.use('/api/auth', authRoutes);
app.use('/api/favorites', favoritesRoutes);
app.use('/api/folders', foldersRoutes);
app.use('/api/series-favorites', seriesFavoritesRouter);
// Statische Dateien bereitstellen
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, 'dist')));
// Setze der MIME-Type für JavaScript-Dateien
app.use((req, res, next) => {
if (req.path.endsWith('.js')) {
res.type('application/javascript');
} else if (req.path.endsWith('.css')) {
res.type('text/css');
}
next();
});
app.get('*', (req, res) => {
if (req.path.startsWith('/assets')) {
const filePath = path.join(__dirname, 'dist', req.path);
if (fs.existsSync(filePath)) {
res.sendFile(filePath);
return;
}
}
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
app.use((req, res, next) => {
if (req.path.endsWith('.js')) {
res.type('application/javascript');
} else if (req.path.endsWith('.css')) {
res.type('text/css');
}
next();
});
// Export für Vercel
export default app;
// Server für lokale Entwicklung
if (process.env.NODE_ENV !== 'production') {
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server läuft lokal auf Port ${PORT}`);
});
}