-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (36 loc) · 1.27 KB
/
server.js
File metadata and controls
42 lines (36 loc) · 1.27 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
const dotenv = require('dotenv').config();
console.log(dotenv);
const express = require('express');
const path = require('path');
// Make sure you import the service function (renamed for general use)
const { getVideos } = require('./YouTubeService');
const PORT = 3000;
const app = express();
const publicPath = path.join(__dirname, 'public');
const middleware = express.static(publicPath);
app.use(middleware);
app.get('/atemlos', async (req, res) => {
try {
// Hardcoded search for the gallery
const videos = await getVideos("Atemlos durch die Nacht");
res.json(videos);
} catch (error) {
res.status(500).json({ error: "API Exception" });
}
});
app.get('/search', async (req, res) => {
const query = req.query.q; // This grabs the text after ?q=
if (!query) {
return res.status(400).json({ error: "No search query provided" });
}
try {
const videos = await getVideos(query);
res.json(videos); // Send the YouTube data back to the browser
} catch (error) {
console.error("YouTube API Exception:", error.message);
res.status(500).json({ error: "Failed to fetch data from YouTube" });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});