Implement the Create User Controller
Create src/controllers/user.controller.js.
The controller receives the HTTP request, calls the appropriate service, and sends the HTTP response.
For user creation, return status code 201 because a new resource has been created.
src/controllers/user.controller.js
import * as userService from '../services/user.service.js';
export async function createUser(req, res, next) {
try {
const user = await userService.createUser(req.body);
res.status(201).json({
success: true,
message: 'User created successfully',
data: user
});
} catch (error) {
next(error);
}
}Calls the user service and returns a 201 response. Errors are passed to the centralized Express error handler.
Key Takeaways
Controllers handle HTTP input and output.
Return 201 after successful resource creation.
Pass unexpected errors to centralized error middleware.