You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo is now configured to run as **Vercel Serverless Functions**:
15
+
16
+
-The Express app lives in **`/api/index.ts`** and **exports the app** (`export default app`), no `app.listen()`.
17
+
-On cold start, we **await DB initialization** before serving requests.
18
+
-API routes return **500** on internal errors (instead of 406) and log a helpful message.
19
+
-A diagnostic route `GET /__debug` is available during testing.
22
20
23
21
---
24
22
25
23
## Project structure
26
24
27
25
```text
26
+
api/
27
+
index.ts # Express app exported for Vercel Functions
28
28
src/
29
29
constants/events.ts
30
30
controllers/Global.ts
31
31
services/
32
-
MongoDB.ts
32
+
MongoDB.ts # MongoDB client (Node driver)
33
33
Users.ts
34
34
Scores.ts
35
-
index.ts
36
35
scripts/
37
36
seed.ts
38
37
```
39
38
40
-
> Note: The seeder populates 15 users with 1–3 scores each (200–500).
41
-
42
39
---
43
40
44
-
## Setup
41
+
## Environment variables
45
42
46
-
1.**Install**
43
+
Create these for **local dev** in a `.env` file at the project root and set the same keys in **Vercel → Project → Settings → Environment Variables** (Production scope):
API_PORT=3000 # used only for local non-serverless runs
50
48
```
51
49
52
-
2.**Environment** — create a `.env` at the project root:
50
+
> For Vercel → Atlas connectivity, Atlas recommends allowing **`0.0.0.0/0`** (all IPs) because Vercel uses **dynamic egress IPs**. Tighten later with Secure Compute / private networking if required. citeturn0search2turn0search7
51
+
52
+
---
53
+
54
+
## Local development
53
55
54
56
```bash
55
-
MONGODB_URI="mongodb://localhost:27017"
56
-
API_PORT=3000
57
+
npm install
58
+
npm run dev # uses `vercel dev` to emulate the platform
57
59
```
58
60
59
-
3.**Run**
61
+
**Seed demo data**
60
62
61
63
```bash
62
-
# dev (watch)
63
-
npm run dev
64
-
65
-
# prod
66
-
npm run build
67
-
npm start
64
+
npm run seed # inserts users + scores
65
+
npm run seed -- --reset # wipe & reseed
68
66
```
69
67
70
-
4.**Seed demo data**
68
+
> The app runs as a Vercel Function locally; you don’t need `npm start` for serverless.
69
+
70
+
---
71
+
72
+
## Deployment (Vercel)
73
+
74
+
**One-time**:
71
75
72
76
```bash
73
-
# inserts 15 users with 1–3 scores each (values 200–500)
74
-
npm run seed
77
+
npm i -g vercel
78
+
vercel login
79
+
```
80
+
81
+
**Deploy**:
75
82
76
-
# wipe & reseed
77
-
npm run seed -- --reset
83
+
```bash
84
+
vercel # Preview deployment
85
+
vercel --prod # Production deployment
78
86
```
79
87
88
+
**Vercel project settings** (for an API-only app):
89
+
90
+
-**Framework Preset**: **Other**
91
+
-**Build Command**: _empty_
92
+
-**Output Directory**: _empty_
93
+
94
+
This avoids the “Missing public directory” error that applies to static sites. If you previously set an Output Directory (e.g., `public/`), clear it. citeturn0search1
-**Express on Vercel**: Files under `/api` become functions; export the Express app and let Vercel handle the server. Don’t call `app.listen()`. citeturn0search0turn0search3
198
+
-**Cold start readiness**: We use an async `ready` promise to **await `initDB()`** on first request.
199
+
-**MongoDB Node driver**: `serverSelectionTimeoutMS` defaults to **30000ms**; we set shorter timeouts to fail fast during testing. citeturn0search14turn0search4
200
+
-**Connection reuse**: Prefer a **singleton client** cached across invocations to avoid reconnect storms in serverless environments. (See `MongoDB.ts`.)
221
201
222
202
---
223
203
224
-
## Notes
204
+
## Troubleshooting
205
+
206
+
### "Missing public directory" during deploy
207
+
208
+
Your project is configured like a static site. For an API-only app, clear **Build Command** and **Output Directory** (Project → Settings). citeturn0search1
Usually network access to Atlas. Ensure Atlas Network Access allows your deployment to connect. For Vercel, allow **`0.0.0.0/0`** during testing (use strong creds), or adopt a fixed-egress solution for production. Also verify you use the **SRV** URI (`mongodb+srv://…`). citeturn0search2turn0search7
213
+
214
+
### Env variables not picked up
215
+
216
+
Set them in **Vercel → Project → Settings → Environment Variables** (correct environment), then redeploy.
217
+
218
+
### Seeing 406 on errors
219
+
220
+
We now return **500** for server errors. If you still see 406, ensure your routes aren’t catching and rethrowing as 406.
221
+
222
+
---
223
+
224
+
## Scripts
225
+
226
+
```json
227
+
{
228
+
"dev": "vercel dev",
229
+
"build": "echo \"No build step for Vercel Functions\"",
230
+
"seed": "tsx src/scripts/seed.ts"
231
+
}
232
+
```
225
233
226
-
- Passwords are hashed with `bcryptjs` (10 rounds).
227
-
- Seeding uses Faker’s `person.fullName()` to generate human names.
228
-
-`tsx` provides fast TypeScript execution with watch mode for development.
234
+
> Vercel builds TypeScript in `/api` automatically; a real `build` step isn’t required for this API-only project. citeturn0search3
0 commit comments