@@ -3,73 +3,79 @@ import fetch from "node-fetch";
33import path from "path" ;
44import { fileURLToPath } from "url" ;
55import dotenv from "dotenv" ;
6- import router from "./route.js" ;
6+ import router from "./route.js" ; // Make sure this file exists
77
88dotenv . config ( ) ;
99
1010const __filename = fileURLToPath ( import . meta. url ) ;
1111const __dirname = path . dirname ( __filename ) ;
1212const GITHUB_TOKEN = process . env . GITHUB_TOKEN ;
1313const app = express ( ) ;
14+ const PORT = process . env . PORT || 3000 ; // Set PORT
1415
1516app . use ( express . static ( path . join ( __dirname , "../" ) ) ) ;
1617
1718app . 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
5051app . 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
7581export default app ;
0 commit comments