Implement Delete User Controller
Add the deleteUser controller.
The controller validates the ID, calls the service, and handles missing users.
After successful deletion, send an empty 204 response.
Delete User Controller
export async function deleteUser(req, res, next) {
try {
const { id } = req.params;
if (!isValidUserId(id)) {
return res.status(400).json({
success: false,
message: 'Invalid user ID'
});
}
const deletedUser = await userService.deleteUser(id);
if (!deletedUser) {
return res.status(404).json({
success: false,
message: 'User not found'
});
}
return res.status(204).send();
} catch (error) {
next(error);
}
}Validates the user identifier, deletes the matching record, and returns 204 after successful deletion.
Key Takeaways
Validate identifiers before deleting.
Handle missing records explicitly.
A 204 response should not contain a response body.