logo

该视频仅会员有权观看

立即开通课程「Node 框架 Express 实践」权限。

¥
199
/ 年

注册时检查用户名是否存在

In the user middleware (src/user/user_middle), code for validating user names is added. The code awaits the result from user_service's get_user_by_name method. If the user exists, it returns an error indicating "user already exists." This error is handled by the default exception handler in app middleware (src/app/app_middle), which is modified to include a new case for the "user already exist" error message, setting the HTTP status code to 409 for conflicts, and the message indicating the username is taken.

When a client attempts to create a user with an already taken username via the HTTP client, the request includes a name attribute for the username and a password. Upon sending the request, a 409 status code is received with a response indicating the username is already in use.