Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported the Project to React and Express #46

Merged
merged 10 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
rahulpoonia29 marked this conversation as resolved.
Show resolved Hide resolved
*.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