Skip to content

Commit

Permalink
Merge pull request #46 from rahulpoonia29/master
Browse files Browse the repository at this point in the history
Ported the Project to React and Express
  • Loading branch information
Satyam1923 authored May 11, 2024
2 parents 02af32e + afc5075 commit 8aa9f66
Show file tree
Hide file tree
Showing 19 changed files with 7,625 additions and 0 deletions.
24 changes: 24 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
98 changes: 98 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
import cors from "cors"; // Import the cors middleware

const cache = new Map();

const app = express();
const port = 3030;
let name = "";

app.use(bodyParser.urlencoded({ extended: true }));

// Use CORS middleware
app.use(cors());

app.get("/", (req, res) => {
res.send("Welcome to the music search API");
});

app.get("/search", async (req, res) => {
name = req.query.song;
console.log("Name is", name);
try {
if (cache.has(name)) {
console.log("Fetching from cache...");
const music = cache.get(name);
res.json(music);
} else {
const response = await axios.get(
`https://jio-savaan-private.vercel.app/api/search/songs?query=${name}`
);
if (
response.data.data.results &&
response.data.data.results.length > 0
) {
const musicArray = response.data.data.results.map((result) => ({
url: result.downloadUrl[4].url,
name: result.name,
year: result.year,
artist: result.artists.primary[0].name,
img: result.image[2].url,
}));
cache.set(name, musicArray);
res.json(musicArray);
} else {
res.json([]);
}
}
} catch (error) {
console.error("Failed to make request:", error.message);
res.status(500).json({
error: "Error fetching song. Please try again later.",
});
}
});

app.get("/search1", async (req, res) => {
console.log(name);
try {
if (cache.has(name)) {
console.log("Fetching from cache...");
const music = cache.get(name);
res.json(music);
} else {
const response = await axios.get(
`https://jio-savaan-private.vercel.app/api/search/songs?query=${name}`
);
if (
response.data.data.results &&
response.data.data.results.length > 0
) {
const musicArray = response.data.data.results
.slice(0, 5)
.map((result) => ({
url: result.downloadUrl[4].url,
name: result.name,
year: result.year,
artist: result.artists.primary[0].name,
img: result.image[2].url,
}));
cache.set(name, musicArray);
res.json(musicArray);
} else {
res.json([]);
}
}
} catch (error) {
console.error("Failed to make request:", error.message);
res.status(500).json({
error: "Error fetching song. Please try again later.",
});
}
});

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Loading

0 comments on commit 8aa9f66

Please sign in to comment.