Spaces:
Paused
Paused
File size: 1,313 Bytes
b689197 10a420a b689197 | 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 | const express = require('express');
const ytSearch = require('yt-search');
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 7860;
// Enable CORS
app.use(cors());
// Serve static files (like HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'public')));
// API Endpoint to search YouTube videos
app.get('/api/search', async (req, res) => {
const query = req.query.q;
if (!query) {
return res.status(400).json({ error: 'Query parameter "q" is required' });
}
try {
const results = await ytSearch(query);
const videos = results.videos.slice(0, 10).map(video => ({
title: video.title,
url: video.url,
duration: video.timestamp,
views: video.views,
uploaded: video.ago,
thumbnail: video.thumbnail
}));
res.json({ videos });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'An error occurred while searching for videos' });
}
});
// Default route to serve the frontend
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
}); |