1
- import express , { Request , Response } from "express" ;
1
+ // src/index.ts
2
+ import express from "express" ;
2
3
import http from "http" ;
3
4
import "dotenv/config" ;
4
- import Global from "./controllers/Global" ;
5
- import EVENTS from "./constants/events" ;
5
+ import Global from "./controllers/Global.ts " ;
6
+ import EVENTS from "./constants/events.ts " ;
6
7
7
8
const app = express ( ) ;
8
9
const server = http . createServer ( app ) ;
9
-
10
10
const PORT = Number ( process . env . API_PORT ) || 3000 ;
11
11
12
12
const globalController = new Global ( ) ;
@@ -18,36 +18,37 @@ globalController
18
18
app . use ( express . json ( ) ) ;
19
19
20
20
app . get ( "/" , ( _req , res ) => {
21
+ console . log ( "GET /" ) ;
21
22
res . send ( "<h1>Hello world</h1>" ) ;
22
23
} ) ;
23
24
24
- // --- Score check endpoint (schema-agnostic): checks against global min, does not persist ---
25
25
app . post (
26
26
"/score" ,
27
- async ( req : Request < { } , { } , { score : number } > , res : Response ) => {
27
+ async (
28
+ req : express . Request < { } , { } , { score : number } > ,
29
+ res : express . Response
30
+ ) => {
28
31
try {
29
32
const { score } = req . body ;
30
33
const result = await globalController . checkScore ( score ) ;
31
- if ( result . message === EVENTS . SCORED ) {
32
- return res . status ( 200 ) . send ( ) ;
33
- }
34
- return res . status ( 409 ) . send ( ) ;
34
+ return result . message === EVENTS . SCORED
35
+ ? res . status ( 200 ) . send ( )
36
+ : res . status ( 409 ) . send ( ) ;
35
37
} catch ( e ) {
36
38
return res . status ( 406 ) . send ( e ) ;
37
39
}
38
40
}
39
41
) ;
40
42
41
- // --- Create user aligned with mobile schema: { userName, password, score? } ---
42
43
app . post (
43
44
"/adduser" ,
44
45
async (
45
- req : Request <
46
+ req : express . Request <
46
47
{ } ,
47
48
{ } ,
48
49
{ userName : string ; password : string ; score ?: number }
49
50
> ,
50
- res : Response
51
+ res : express . Response
51
52
) => {
52
53
try {
53
54
const { userName, password, score } = req . body ;
@@ -56,49 +57,39 @@ app.post(
56
57
password,
57
58
score,
58
59
} ) ;
59
-
60
- if ( result . message === EVENTS . USER_ALREADY_EXIST ) {
60
+ if ( result . message === EVENTS . USER_ALREADY_EXIST )
61
61
return res . status ( 409 ) . send ( "User Already Exist." ) ;
62
- }
63
- if ( result . message === EVENTS . USER_CREATED ) {
62
+ if ( result . message === EVENTS . USER_CREATED )
64
63
return res . status ( 200 ) . json ( result . list ) ;
65
- }
66
64
return res . send ( ) ;
67
65
} catch ( e ) {
68
- console . log ( "index - response 406" ) ;
69
66
return res . status ( 406 ) . send ( e ) ;
70
67
}
71
68
}
72
69
) ;
73
70
74
- // --- Add a score for a specific user ---
75
71
app . post (
76
72
"/users/:userName/scores" ,
77
73
async (
78
- req : Request < { userName : string } , { } , { value : number } > ,
79
- res : Response
74
+ req : express . Request < { userName : string } , { } , { value : number } > ,
75
+ res : express . Response
80
76
) => {
81
77
try {
82
78
const { userName } = req . params ;
83
79
const { value } = req . body ;
84
-
85
- if ( typeof value !== "number" ) {
80
+ if ( typeof value !== "number" )
86
81
return res . status ( 400 ) . send ( "value must be a number" ) ;
87
- }
88
-
89
82
const result = await globalController . addScoreForUser ( userName , value ) ;
90
- if ( result . message === EVENTS . SCORED && result . created ) {
91
- return res . status ( 201 ) . json ( { userId : result . userId , value } ) ;
92
- }
93
- return res . status ( 409 ) . send ( ) ;
83
+ return result . message === EVENTS . SCORED && result . created
84
+ ? res . status ( 201 ) . json ( { userId : result . userId , value } )
85
+ : res . status ( 409 ) . send ( ) ;
94
86
} catch ( e ) {
95
87
return res . status ( 406 ) . send ( e ) ;
96
88
}
97
89
}
98
90
) ;
99
91
100
- // --- Get all users (public) ---
101
- app . get ( "/users" , async ( _req : Request , res : Response ) => {
92
+ app . get ( "/users" , async ( _req , res ) => {
102
93
try {
103
94
const list = await globalController . listUsersPublic ( ) ;
104
95
return res . status ( 200 ) . json ( list ) ;
@@ -107,17 +98,18 @@ app.get("/users", async (_req: Request, res: Response) => {
107
98
}
108
99
} ) ;
109
100
110
- // --- Get top N scores with user (default 10) ---
111
101
app . get (
112
102
"/scores/top" ,
113
- async ( req : Request < { } , { } , { } , { limit ?: string } > , res : Response ) => {
103
+ async (
104
+ req : express . Request < { } , { } , { } , { limit ?: string } > ,
105
+ res : express . Response
106
+ ) => {
114
107
try {
115
108
const raw = req . query . limit ;
116
109
const parsed = raw ? parseInt ( raw , 10 ) : 10 ;
117
110
const limit = Number . isFinite ( parsed )
118
111
? Math . min ( Math . max ( parsed , 1 ) , 50 )
119
112
: 10 ;
120
-
121
113
const rows = await globalController . getTopScores ( limit ) ;
122
114
return res . status ( 200 ) . json ( rows ) ;
123
115
} catch ( e ) {
@@ -126,6 +118,11 @@ app.get(
126
118
}
127
119
) ;
128
120
129
- server . listen ( PORT , ( ) => {
130
- console . log ( `Listening on ${ PORT } ` ) ;
131
- } ) ;
121
+ // ❗ run the HTTP listener only when NOT on Vercel
122
+ // if (!process.env.VERCEL_ENV) {
123
+ // console.log(`Listening on port ${PORT}`);
124
+ server . listen ( PORT , ( ) => console . log ( `Listening on ${ PORT } ` ) ) ;
125
+ // }
126
+
127
+ // ✅ expose the Express app for Vercel's /api entry
128
+ export default app ;
0 commit comments