One rule
Your status code should tell the client what happened before they read the JSON.
Use 2xx for success, 4xx when the client sent a bad request, and 5xx when the server failed.
Why it matters
Recruiters will run your API with curl, Postman, or Thunder Client. They should see useful status codes, not only pretty messages.
A frontend also depends on this. If your API returns 200 for an error, the frontend may treat a failed request as a success.
Common choices
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Use status then json
In Express 5, write the status first, then send JSON. Older tutorials may show removed shortcuts like res.json(obj, status). Do not use those.
app.post('/students', express.json(), (req, res) => {
const { name, college } = req.body;
if (!name || !college) {
return res.status(400).json({
error: 'name and college are required'
});
}
const student = {
id: 'stu_101',
name,
college
};
return res.status(201).json({
message: 'student created',
student
});
});Not found example
Use 404 when the URL is valid but the resource is missing. For example, the route exists, but that project ID is not in your data.
app.get('/projects/:id', (req, res) => {
const project = projects.find((item) => item.id === req.params.id);
if (!project) {
return res.status(404).json({
error: 'project not found'
});
}
return res.status(200).json({ project });
});Avoid fake success
Do not return 200 with { error: 'missing name' }. That makes clients guess what happened.
Also avoid using 500 for validation mistakes. If the user forgot a field, the server did not crash. The client sent a bad request, so use 400.
Ask AI carefully
If you use ChatGPT, Gemini, or Claude to draft route handlers, ask it to include meaningful HTTP status codes. Then check every response yourself.
Look for old Express patterns. In Express 5, use res.status(status).json(obj) and res.status(status).send(body).
Prompt:
Write an Express 5 route for POST /registrations.
Use express.json(), validate name and email, return 400 for missing fields, 201 for created, and JSON responses.
Do not use old res.json(obj, status) syntax.Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done