Skip to content

Commit 96cdbec

Browse files
committed
review(server): add comments to server.js
1 parent 0bb005e commit 96cdbec

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

server/server.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
i'd recommend organizing your imports at the top of files (here and in other files),
3+
perhaps by sections separated with spaces, i.e.:
4+
5+
// library dependencies
6+
import express from 'express'
7+
import cors from 'cors'
8+
import helmet from 'helmet'
9+
import { z } from 'zod'
10+
...
11+
12+
// routes
13+
import mcpRoutes from './routes/mcp.js'
14+
import agentRoutes from './routes/agent.js'
15+
...
16+
17+
// helper functions / constants / other data / etc.
18+
import { healthCheck } from './db.js'
19+
import { query } from './db.js'
20+
...
21+
22+
all up to you how you want to do this. but i find it helps with readability and organization.
23+
*/
24+
125
import 'dotenv/config';
226
import express from 'express';
327
import cors from 'cors';
@@ -27,13 +51,17 @@ app.use(morgan('dev'));
2751
app.use(cookieParser());
2852

2953
// --- Request Logging Middleware ---
30-
app.use((req, res, next) => {
54+
55+
// a convention you can choose to follow is prefixing unused parameters with an underscore
56+
app.use((req, _res, next) => {
3157
const user = req.headers['x-user-id'] || 'anonymous';
3258
console.log(
3359
`[${new Date().toISOString()}] ${req.method} ${
3460
req.originalUrl
3561
} | user=${user}`
3662
);
63+
// ^ nice logging; this is great.
64+
3765
next();
3866
});
3967

@@ -50,17 +78,21 @@ app.get('/db/ping', async (_req, res) => {
5078
}
5179
});
5280

81+
// Mount users route at /users
82+
// ^ imo, this kind of comment is a bit useless: it's obvious to other devs what it does :)
83+
app.use('/', userRouter);
84+
85+
// i'd probably put the other routes here as well.
86+
5387
/** Users */
5488
const UserBody = z.object({
5589
email: z.string().email(),
5690
github_username: z.string().min(1).optional(),
5791
});
58-
// Mount users route at /users
59-
app.use('/', userRouter);
6092

6193
// Create or upsert user by email
6294
app.post('/users', async (req, res) => {
63-
const parse = UserBody.safeParse(req.body);
95+
const parse = UserBody.safeParse(req.body); // love that you are doing this. great.
6496
if (!parse.success)
6597
return res.status(400).json({ error: parse.error.message });
6698
const { email, github_username } = parse.data;
@@ -82,6 +114,9 @@ app.post('/users', async (req, res) => {
82114
}
83115
});
84116

117+
// you definitely want to minimize commented-out code like below
118+
// if you don't need it, just remove it.
119+
85120
// app.get('/users', async (_req, res) => {
86121
// try {
87122
// const rows = await query(
@@ -125,30 +160,28 @@ app.get('/connections', async (_req, res) => {
125160
}
126161
});
127162

128-
// // --- Request Logging Middleware ---
129-
// app.use((req, res, next) => {
130-
// const user = req.headers['x-user-id'] || 'anonymous';
131-
// console.log(
132-
// `[${new Date().toISOString()}] ${req.method} ${
133-
// req.originalUrl
134-
// } | user=${user}`
135-
// );
136-
// next();
137-
// });
138-
139163
// -- Agent entry point
164+
165+
/*
166+
you should keep your router names consistent:
167+
- deploymentsRouter
168+
- agentRouter (not agentRoutes)
169+
- authAwsRouter (not authAws)
170+
- authGoogleRouter (not authGoogle)
171+
etc.
172+
*/
173+
174+
// also, i'd probably move these routes closer to the top of the file, so they're easier to find.
175+
140176
app.use('/deployments', deploymentsRouter);
141177
app.use('/agent', agentRoutes);
142178
app.use('/mcp/v1', pipelineCommitRouter);
143179
app.use('/mcp/v1', mcpRoutes);
144180

145-
// Mount GitHub OAuth routes at /auth/github
146181
app.use('/auth/github', githubAuthRouter);
147182
app.use(authRoutes);
148-
// Mount AWS SSO routes
149183
app.use('/auth/aws', authAws);
150184

151-
// Mount Google OAuth routes
152185
app.use('/auth/google', authGoogle);
153186

154187
app.use('/jenkins', jenkinsRouter);

0 commit comments

Comments
 (0)