Skip to content
Merged
Changes from all 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
104 changes: 55 additions & 49 deletions Website/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,79 @@ import fetch from "node-fetch";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import router from "./route.js";
import router from "./route.js"; // Make sure this file exists

dotenv.config();

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const app = express();
const PORT = process.env.PORT || 3000; // Set PORT

app.use(express.static(path.join(__dirname, "../")));

app.get("/api/github/repos/subdir", async (req, res) => {
const dirName = req.query.dir;
if (!dirName) {
return res.status(400).json({ error: "Directory name is required" });
}

try {
const response = await fetch(
`https://api.github.com/repos/recodehive/machine-learning-repos/contents/${dirName}`,
{
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
},
}
);
if (!response.ok) {
const errorDetails = await response.text();
throw new Error(
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
);
const dirName = req.query.dir;
if (!dirName) {
return res.status(400).json({ error: "Directory name is required" });
}

const data = await response.json();
res.json(data);
} catch (error) {
console.error(
`Error fetching GitHub subdirectory contents for ${dirName}:`,
error
);
res.status(500).json({ error: error.message });
}
try {
const response = await fetch(
`https://api.github.com/repos/recodehive/machine-learning-repos/contents/${dirName}`,
{
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
},
}
);
if (!response.ok) {
const errorDetails = await response.text();
throw new Error(
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
);
}

const data = await response.json();
res.json(data);
} catch (error) {
console.error(
`Error fetching GitHub subdirectory contents for ${dirName}:`,
error
);
res.status(500).json({ error: error.message });
}
});

app.get("/api/github/repos", async (req, res) => {
try {
const response = await fetch(
"https://api.github.com/repos/recodehive/machine-learning-repos/contents/",
{
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
},
}
);
if (!response.ok) {
const errorDetails = await response.text();
throw new Error(
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
);
try {
const response = await fetch(
"https://api.github.com/repos/recodehive/machine-learning-repos/contents/",
{
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
},
}
);
if (!response.ok) {
const errorDetails = await response.text();
throw new Error(
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
);
}

const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching GitHub directories:", error);
res.status(500).json({ error: error.message });
}
});

const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching GitHub directories:", error);
res.status(500).json({ error: error.message });
}
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

export default app;
Loading