Skip to content

Commit b4840ff

Browse files
authored
Merge pull request #1458 from shreyasparaj/main
Add server listen functionality to start Express server
2 parents 83f6ec2 + f78f5e1 commit b4840ff

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

Website/server/server.js

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,79 @@ import fetch from "node-fetch";
33
import path from "path";
44
import { fileURLToPath } from "url";
55
import dotenv from "dotenv";
6-
import router from "./route.js";
6+
import router from "./route.js"; // Make sure this file exists
77

88
dotenv.config();
99

1010
const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = path.dirname(__filename);
1212
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
1313
const app = express();
14+
const PORT = process.env.PORT || 3000; // Set PORT
1415

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

1718
app.get("/api/github/repos/subdir", async (req, res) => {
18-
const dirName = req.query.dir;
19-
if (!dirName) {
20-
return res.status(400).json({ error: "Directory name is required" });
21-
}
22-
23-
try {
24-
const response = await fetch(
25-
`https://api.github.com/repos/recodehive/machine-learning-repos/contents/${dirName}`,
26-
{
27-
headers: {
28-
Authorization: `Bearer ${GITHUB_TOKEN}`,
29-
},
30-
}
31-
);
32-
if (!response.ok) {
33-
const errorDetails = await response.text();
34-
throw new Error(
35-
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
36-
);
19+
const dirName = req.query.dir;
20+
if (!dirName) {
21+
return res.status(400).json({ error: "Directory name is required" });
3722
}
3823

39-
const data = await response.json();
40-
res.json(data);
41-
} catch (error) {
42-
console.error(
43-
`Error fetching GitHub subdirectory contents for ${dirName}:`,
44-
error
45-
);
46-
res.status(500).json({ error: error.message });
47-
}
24+
try {
25+
const response = await fetch(
26+
`https://api.github.com/repos/recodehive/machine-learning-repos/contents/${dirName}`,
27+
{
28+
headers: {
29+
Authorization: `Bearer ${GITHUB_TOKEN}`,
30+
},
31+
}
32+
);
33+
if (!response.ok) {
34+
const errorDetails = await response.text();
35+
throw new Error(
36+
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
37+
);
38+
}
39+
40+
const data = await response.json();
41+
res.json(data);
42+
} catch (error) {
43+
console.error(
44+
`Error fetching GitHub subdirectory contents for ${dirName}:`,
45+
error
46+
);
47+
res.status(500).json({ error: error.message });
48+
}
4849
});
4950

5051
app.get("/api/github/repos", async (req, res) => {
51-
try {
52-
const response = await fetch(
53-
"https://api.github.com/repos/recodehive/machine-learning-repos/contents/",
54-
{
55-
headers: {
56-
Authorization: `Bearer ${GITHUB_TOKEN}`,
57-
},
58-
}
59-
);
60-
if (!response.ok) {
61-
const errorDetails = await response.text();
62-
throw new Error(
63-
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
64-
);
52+
try {
53+
const response = await fetch(
54+
"https://api.github.com/repos/recodehive/machine-learning-repos/contents/",
55+
{
56+
headers: {
57+
Authorization: `Bearer ${GITHUB_TOKEN}`,
58+
},
59+
}
60+
);
61+
if (!response.ok) {
62+
const errorDetails = await response.text();
63+
throw new Error(
64+
`GitHub API error: ${response.status} - ${response.statusText}: ${errorDetails}`
65+
);
66+
}
67+
68+
const data = await response.json();
69+
res.json(data);
70+
} catch (error) {
71+
console.error("Error fetching GitHub directories:", error);
72+
res.status(500).json({ error: error.message });
6573
}
74+
});
6675

67-
const data = await response.json();
68-
res.json(data);
69-
} catch (error) {
70-
console.error("Error fetching GitHub directories:", error);
71-
res.status(500).json({ error: error.message });
72-
}
76+
// Start the server
77+
app.listen(PORT, () => {
78+
console.log(`Server is running on http://localhost:${PORT}`);
7379
});
7480

7581
export default app;

0 commit comments

Comments
 (0)