11const express = require ( "express" ) ;
22const cors = require ( "cors" ) ;
3- const path = require ( "path" ) ;
4- const fs = require ( "fs" ) ;
3+
54const app = express ( ) ;
65const PORT = process . env . PORT || 3000 ;
76
87app . use ( cors ( ) ) ;
9- app . use ( express . static ( path . join ( __dirname , "frontend" ) ) ) ;
8+ app . use ( express . static ( "frontend" ) ) ;
109
10+ /* ---------------- HOME ROUTES ---------------- */
1111app . get ( "/" , ( req , res ) => {
12- res . sendFile ( path . join ( __dirname , "frontend" , " index.html") ) ;
12+ res . sendFile ( __dirname + "/frontend/ index.html") ;
1313} ) ;
1414
1515app . get ( "/leaderboard" , ( req , res ) => {
16- res . sendFile ( path . join ( __dirname , "frontend" , " leaderboard.html") ) ;
16+ res . sendFile ( __dirname + "/frontend/ leaderboard.html") ;
1717} ) ;
1818
1919app . get ( "/about" , ( req , res ) => {
20- res . sendFile ( path . join ( __dirname , "frontend" , " about.html") ) ;
20+ res . sendFile ( __dirname + "/frontend/ about.html") ;
2121} ) ;
2222
2323app . get ( "/registration" , ( req , res ) => {
24- res . sendFile ( path . join ( __dirname , "frontend" , " registration.html") ) ;
24+ res . sendFile ( __dirname + "/frontend/ registration.html") ;
2525} ) ;
2626
2727app . get ( "/uptime" , ( req , res ) => {
2828 res . json ( { status : "Website is running ✅" } ) ;
2929} ) ;
3030
31+ /* ---------------- API: STUDENT HISTORY ---------------- */
32+ app . get ( "/api/student/:username" , async ( req , res ) => {
33+ const { username } = req . params ;
34+
35+ try {
36+ console . log ( "Fetching history for:" , username ) ;
37+
38+ // 1. Get list of all daily files from GitHub repo
39+ const apiURL =
40+ "https://api.github.com/repos/codepvg/leetcode-ranking-data/contents/daily?ref=main" ;
41+
42+ const response = await fetch ( apiURL ) ;
43+ const files = await response . json ( ) ;
44+
45+
46+ if ( ! Array . isArray ( files ) ) {
47+ return res . status ( 500 ) . json ( {
48+ error : "GitHub API failed" ,
49+ details : files . message || files ,
50+ } ) ;
51+ }
52+
53+ let history = [ ] ;
54+
55+ // 2. Loop through each file
56+ for ( const file of files ) {
57+ if ( ! file . name . endsWith ( ".json" ) ) continue ;
58+
59+ const fileRes = await fetch ( file . download_url ) ;
60+ const data = await fileRes . json ( ) ;
61+
62+ const user = data . find ( ( u ) => u . id === username ) ;
63+
64+ if ( user ) {
65+ const date = file . name . split ( "-" ) . slice ( 0 , 3 ) . join ( "-" ) ;
66+
67+ history . push ( {
68+ date,
69+ easy : user . data . easySolved ,
70+ medium : user . data . mediumSolved ,
71+ hard : user . data . hardSolved ,
72+ } ) ;
73+ }
74+ }
75+
76+ // 3. Sort by date
77+ history . sort ( ( a , b ) => new Date ( a . date ) - new Date ( b . date ) ) ;
78+
79+ res . json ( {
80+ username,
81+ history,
82+ } ) ;
83+ } catch ( err ) {
84+ console . error ( err ) ;
85+ res . status ( 500 ) . json ( {
86+ error : "Failed to fetch student history" ,
87+ details : err . message ,
88+ } ) ;
89+ }
90+ } ) ;
91+
92+ /* ---------------- 404 ---------------- */
3193app . use ( ( req , res ) => {
3294 res . status ( 404 ) . send ( "Page not found" ) ;
3395} ) ;
3496
97+ /* ---------------- START ---------------- */
3598app . listen ( PORT , ( ) => {
3699 console . log ( `Server running at http://localhost:${ PORT } ` ) ;
37- } ) ;
100+ } ) ;
0 commit comments