@@ -3,73 +3,79 @@ import fetch from "node-fetch";
3
3
import path from "path" ;
4
4
import { fileURLToPath } from "url" ;
5
5
import dotenv from "dotenv" ;
6
- import router from "./route.js" ;
6
+ import router from "./route.js" ; // Make sure this file exists
7
7
8
8
dotenv . config ( ) ;
9
9
10
10
const __filename = fileURLToPath ( import . meta. url ) ;
11
11
const __dirname = path . dirname ( __filename ) ;
12
12
const GITHUB_TOKEN = process . env . GITHUB_TOKEN ;
13
13
const app = express ( ) ;
14
+ const PORT = process . env . PORT || 3000 ; // Set PORT
14
15
15
16
app . use ( express . static ( path . join ( __dirname , "../" ) ) ) ;
16
17
17
18
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" } ) ;
37
22
}
38
23
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
+ }
48
49
} ) ;
49
50
50
51
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 } ) ;
65
73
}
74
+ } ) ;
66
75
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 } ` ) ;
73
79
} ) ;
74
80
75
81
export default app ;
0 commit comments