From 23d6cba0a1c9dcee12404909a2671e606b3c416c Mon Sep 17 00:00:00 2001 From: AstronDaniel Date: Sat, 7 Dec 2024 18:43:49 +0300 Subject: [PATCH 1/6] Update API endpoints to use environment variables, enhance CORS configuration, and adjust server port --- auth.js | 4 ++-- homepage.js | 8 ++++---- server.js | 13 +++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/auth.js b/auth.js index 040d941..af8f522 100644 --- a/auth.js +++ b/auth.js @@ -17,7 +17,7 @@ async function handleSignUp(event) { }; try { - const response = await fetch('http://localhost:5000/api/users/register', { + const response = await fetch('${process.env.REACT_APP_API_URL}users/register', { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -54,7 +54,7 @@ async function handleLogin(event) { }; try { - const response = await fetch('http://localhost:5000/api/users/login', { + const response = await fetch('${process.env.REACT_APP_API_URL}users/login', { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/homepage.js b/homepage.js index 8814918..61615d7 100644 --- a/homepage.js +++ b/homepage.js @@ -129,7 +129,7 @@ const musicDownloader = { } }); - const downloadUrl = `http://localhost:5000/api/songs/download?videoUrl=${encodeURIComponent(videoUrl)}`; + const downloadUrl = `${process.env.REACT_APP_API_URL}songs/download?videoUrl=${encodeURIComponent(videoUrl)}`; // Use fetch with timeout const response = await fetch(downloadUrl, { @@ -164,7 +164,7 @@ const musicDownloader = { // Fetch and render latest music async function fetchAndRenderLatestMusic() { try { - const response = await fetch('http://localhost:5000/api/songs/latest'); + const response = await fetch('${process.env.REACT_APP_API_URL}songs/latest'); const data = await response.json(); if (response.ok) { @@ -181,7 +181,7 @@ async function fetchAndRenderLatestMusic() { // Fetch and render trending music async function fetchAndRenderTrendingMusic() { try { - const response = await fetch('http://localhost:5000/api/songs/trending'); + const response = await fetch('${process.env.REACT_APP_API_URL}songs/trending'); const data = await response.json(); if (response.ok) { @@ -304,7 +304,7 @@ document.addEventListener('DOMContentLoaded', () => { const searchHandler = { async searchSongs(query) { try { - const response = await fetch(`http://localhost:5000/api/songs/search?query=${encodeURIComponent(query)}`); + const response = await fetch(`${process.env.REACT_APP_API_URL}songs/search?query=${encodeURIComponent(query)}`); const data = await response.json(); if (response.ok) { diff --git a/server.js b/server.js index f4ff2f8..37517f7 100644 --- a/server.js +++ b/server.js @@ -2,12 +2,16 @@ const mongoose = require('mongoose'); const express = require('express'); const cors = require('cors'); -const bodyParser = require('body-parser'); require('dotenv').config(); // For environment variables const app = express(); -app.use(cors()); -app.use(bodyParser.json()); // For parsing application/json + +app.use(cors({ + origin: ['https://beatwave13.netlify.app', 'http://localhost:3000'], + credentials: true +})); + +app.use(express.json()); // MongoDB Atlas connection URI const uri = process.env.MONGODB_URI; // Store your connection string in .env @@ -27,8 +31,9 @@ const songRoutes = require('./api/routes/songRoutes'); // Ensure the correct pat // Use routes app.use('/api/songs', songRoutes); +app.use('/api/users', require('./api/routes/userRoutes')); -const PORT = process.env.PORT || 5000; +const PORT = process.env.PORT || 10000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); \ No newline at end of file From 3ccf028789043c06c1217024d8e96dc9f3c08c4e Mon Sep 17 00:00:00 2001 From: AstronDaniel Date: Sat, 7 Dec 2024 18:50:23 +0300 Subject: [PATCH 2/6] Fix deployment structure --- .env | 2 +- netlify.toml | 9 +++++++-- package.json | 18 ++++++++++++++++++ about.css => public/about.css | 0 about.html => public/about.html | 0 auth.css => public/auth.css | 0 auth.js => public/auth.js | 0 homepage.css => public/homepage.css | 0 homepage.js => public/homepage.js | 0 index.html => public/index.html | 0 login.html => public/login.html | 0 musicServer.js => public/musicServer.js | 0 signup.html => public/signup.html | 0 13 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 package.json rename about.css => public/about.css (100%) rename about.html => public/about.html (100%) rename auth.css => public/auth.css (100%) rename auth.js => public/auth.js (100%) rename homepage.css => public/homepage.css (100%) rename homepage.js => public/homepage.js (100%) rename index.html => public/index.html (100%) rename login.html => public/login.html (100%) rename musicServer.js => public/musicServer.js (100%) rename signup.html => public/signup.html (100%) diff --git a/.env b/.env index 71cb538..9ebea02 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ MONGODB_URI=mongodb+srv://Astrav:sempala152@beatwave.rr74j.mongodb.net/beatwave?retryWrites=true&w=majority&appName=beatwave -REACT_APP_API_URL=https://beatwave-api.onrender.com \ No newline at end of file +REACT_APP_API_URL=https://beat-wave.onrender.com \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index 76d6769..34d90cd 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,6 +1,11 @@ [build] - publish = "/" - command = "# no build command needed" + publish = "public" + command = "npm install" + +[[redirects]] + from = "/api/*" + to = "https://beat-wave.onrender.com/api/:splat" + status = 200 [[redirects]] from = "/*" diff --git a/package.json b/package.json new file mode 100644 index 0000000..7299137 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "beatwave", + "version": "1.0.0", + "description": "Music streaming application", + "main": "server.js", + "scripts": { + "start": "node server.js", + "dev": "nodemon server.js" + }, + "dependencies": { + "express": "^4.18.2", + "cors": "^2.8.5", + "dotenv": "^16.3.1", + "mongoose": "^7.6.3", + "ytdl-core": "^4.11.5", + "axios": "^1.6.2" + } +} \ No newline at end of file diff --git a/about.css b/public/about.css similarity index 100% rename from about.css rename to public/about.css diff --git a/about.html b/public/about.html similarity index 100% rename from about.html rename to public/about.html diff --git a/auth.css b/public/auth.css similarity index 100% rename from auth.css rename to public/auth.css diff --git a/auth.js b/public/auth.js similarity index 100% rename from auth.js rename to public/auth.js diff --git a/homepage.css b/public/homepage.css similarity index 100% rename from homepage.css rename to public/homepage.css diff --git a/homepage.js b/public/homepage.js similarity index 100% rename from homepage.js rename to public/homepage.js diff --git a/index.html b/public/index.html similarity index 100% rename from index.html rename to public/index.html diff --git a/login.html b/public/login.html similarity index 100% rename from login.html rename to public/login.html diff --git a/musicServer.js b/public/musicServer.js similarity index 100% rename from musicServer.js rename to public/musicServer.js diff --git a/signup.html b/public/signup.html similarity index 100% rename from signup.html rename to public/signup.html From 8a5dcdcecfcc41eeb02f92159f725197d8bdf996 Mon Sep 17 00:00:00 2001 From: AstronDaniel Date: Sat, 7 Dec 2024 19:03:27 +0300 Subject: [PATCH 3/6] Add WebSocket support, update package dependencies, and refactor server initialization --- package.json | 10 +++++++++- server.js | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 7299137..8506058 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,14 @@ "dotenv": "^16.3.1", "mongoose": "^7.6.3", "ytdl-core": "^4.11.5", - "axios": "^1.6.2" + "axios": "^1.6.2", + "ws": "^8.16.0", + "spotify-web-api-node": "^5.0.2" + }, + "devDependencies": { + "nodemon": "^3.0.2" + }, + "engines": { + "node": "18.x" } } \ No newline at end of file diff --git a/server.js b/server.js index 37517f7..fdae636 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,14 @@ // server.js -const mongoose = require('mongoose'); const express = require('express'); const cors = require('cors'); -require('dotenv').config(); // For environment variables +const mongoose = require('mongoose'); +const WebSocket = require('ws'); +const http = require('http'); +require('dotenv').config(); const app = express(); +const server = http.createServer(app); +const wss = new WebSocket.Server({ server }); app.use(cors({ origin: ['https://beatwave13.netlify.app', 'http://localhost:3000'], @@ -13,27 +17,29 @@ app.use(cors({ app.use(express.json()); -// MongoDB Atlas connection URI -const uri = process.env.MONGODB_URI; // Store your connection string in .env - -// Connect to MongoDB with increased timeout settings -mongoose.connect(uri, { - serverSelectionTimeoutMS: 30000, // Increase timeout to 30 seconds - socketTimeoutMS: 45000 // Increase socket timeout to 45 seconds -}).then(() => { - console.log("Connected to MongoDB!"); -}).catch(err => { - console.error("Error connecting to MongoDB:", err); +// WebSocket connection handling +wss.on('connection', (ws) => { + ws.id = Math.random().toString(36).substr(2, 9); + console.log('New client connected'); + + ws.on('close', () => { + console.log('Client disconnected'); + }); }); -// Import routes -const songRoutes = require('./api/routes/songRoutes'); // Ensure the correct path +// Add WebSocket server to app +app.set('wss', wss); -// Use routes -app.use('/api/songs', songRoutes); +// Routes +app.use('/api/songs', require('./api/routes/songRoutes')); app.use('/api/users', require('./api/routes/userRoutes')); const PORT = process.env.PORT || 10000; -app.listen(PORT, () => { - console.log(`Server is running on port ${PORT}`); -}); \ No newline at end of file + +mongoose.connect(process.env.MONGODB_URI) + .then(() => { + server.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); + }); + }) + .catch(err => console.error('MongoDB connection error:', err)); \ No newline at end of file From a3ed5ae7d3f4ff092ff73fbfd4abba028fdfed9f Mon Sep 17 00:00:00 2001 From: AstronDaniel Date: Sat, 7 Dec 2024 20:35:02 +0300 Subject: [PATCH 4/6] Add API_URL constant for backend integration in auth and homepage scripts --- public/auth.js | 2 ++ public/homepage.js | 1 + 2 files changed, 3 insertions(+) diff --git a/public/auth.js b/public/auth.js index af8f522..5fc0cad 100644 --- a/public/auth.js +++ b/public/auth.js @@ -1,5 +1,7 @@ // Mock Database (You can replace this with real backend integration later) const users = []; +// auth.js +const API_URL = 'https://beat-wave.onrender.com'; // Handle Sign-Up async function handleSignUp(event) { diff --git a/public/homepage.js b/public/homepage.js index 61615d7..863831c 100644 --- a/public/homepage.js +++ b/public/homepage.js @@ -1,4 +1,5 @@ let isLoggedIn = false; // Track login state +const API_URL = 'https://beat-wave.onrender.com'; // Utility Functions const utils = { From fd14f6a74ef13e333deba2f8dffa3727e683e063 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 19 Nov 2025 05:34:35 +0000 Subject: [PATCH 5/6] Initial plan From db96f459b7589a42a5f393c10f735c459f8f156b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 19 Nov 2025 05:36:30 +0000 Subject: [PATCH 6/6] Initial plan