Skip to content

Commit

Permalink
Merge pull request Satyam1923#210 from Yash-Ainapure/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Satyam1923 authored May 31, 2024
2 parents 8963a94 + 9d7a8bb commit d26c460
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 347 deletions.
61 changes: 41 additions & 20 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
import fetch from 'node-fetch';

import cors from "cors" ;
import cors from "cors";


const cache = new Map();
Expand All @@ -19,22 +20,32 @@ app.get("/", (req, res) => {
res.send("Welcome to the music search API");
});

app.get("/search", async (req, res) => {
const song = req.query.song;
const language = req.query.language;
console.log("Name is", song);
console.log("Language is", language);


app.get('/search', async (req, res) => {
try {
if (cache.has(song)) {
console.log("Fetching from cache...");
const music = cache.get(song);
const filteredMusic = language === "all" ? music : music.filter(m => m.language === language);
res.json(filteredMusic);
} else {
const response = await axios.get(
`https://jio-savaan-private.vercel.app/api/search/songs?query=${song}`
);
const { song } = req.query;
const language = req.query.language;
console.log("Name is", song);
// console.log("Language is", language);

const apiUrl = `https://jio-savaan-private.vercel.app/api/search/songs?query=${encodeURIComponent(song)}`;
console.log(apiUrl);


//correct till here,now manipulate data and send it:
try {

const response = await fetch(apiUrl);

if (!response.ok) {
throw new Error('Failed to fetch data from the external API');
}

const data = await response.json();

if (
response && response.data && response.data.data &&
response.data.data.results &&
response.data.data.results.length > 0
) {
Expand All @@ -50,21 +61,31 @@ app.get("/search", async (req, res) => {
img: result.image[2]?.url || "",
language: result.language || ""
}));
cache.set(song, musicArray);
// cache.set(song, musicArray);
const filteredMusic = language === "all" ? musicArray : musicArray.filter(m => m.language === language);
res.json(filteredMusic);
} else {
console.log("response.data.data");
console.log(response);
res.json([]);
}

} catch (error) {
console.log("gjdjss error");
console.log(error);
res.json([]);
}


// res.json(data);
} catch (error) {
console.error("Failed to make request:", error.message);
res.status(500).json({
error: "Error fetching song. Please try again later.",
});
// Handle errors
console.error('Error occurred while processing the request:', error);
res.status(500).json({ error: 'Internal server error' });
}
});


app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
93 changes: 92 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"express": "^4.18.3"
"express": "^4.18.3",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"nodemon": "^3.1.0"
Expand Down
Loading

0 comments on commit d26c460

Please sign in to comment.