When implementing an Express JSON endpoint:
- •Keep endpoints minimal (prefer a single route over multiple routes when feasible).
- •Always add
app.use(express.json())before JSON routes. - •Validate required inputs early and return
400with{ error: string }. - •Separate concerns lightly:
- •validate inputs
- •run core logic
- •return JSON
- •Status codes:
- •
200for success - •
400for invalid input - •
500for unexpected failures
- •
- •Error payload shape:
- •
{ error: "Human-readable message" }
- •
- •Do not leak sensitive details to the client; log details server-side.
Output expectations:
- •Provide the minimal code changes needed.
- •Avoid introducing frameworks, databases, or extra layers unless asked.