-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (42 loc) · 1.38 KB
/
server.js
File metadata and controls
52 lines (42 loc) · 1.38 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
44
45
46
47
48
49
50
51
52
const express = require('express');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const app = express();
const port = 3000;
const mediaDirectory = path.join(__dirname, 'media');
const websiteDirectory = path.join(__dirname, 'website');
// Statische Website-Dateien ausliefern
app.use(express.static(websiteDirectory));
// Middleware für JSON-Parsing
app.use(express.json());
// Medienverzeichnis beobachten und Dateiliste automatisch aktualisieren
let mediaFiles = [];
function updateMediaFiles() {
mediaFiles = fs.readdirSync(mediaDirectory).filter(file => {
const ext = path.extname(file).toLowerCase();
return ['.mp4', '.jpg', '.png', '.jpeg'].includes(ext);
}).map(file => {
return {
type: ['.mp4'].includes(path.extname(file).toLowerCase()) ? 'video' : 'image',
name: file,
src: `/media/${file}`
};
});
}
// Dateien überwachen
chokidar.watch(mediaDirectory).on('all', () => {
updateMediaFiles();
});
// Statische Medien-Dateien bereitstellen
app.use('/media', express.static(mediaDirectory));
// Endpunkt, um die Liste der Medien abzurufen
app.get('/api/media', (req, res) => {
res.json(mediaFiles);
});
// Initiale Aktualisierung
updateMediaFiles();
// Server starten
app.listen(port, () => {
console.log(`Server läuft auf http://localhost:${port}`);
});