Define a model named User in a file named User.js in your API server’s src/users/models/ directory. The schema for the model should contain the following keys.
- username (String)
- password (String)
- firstName (String)
- lastName (String)
- email (String)
Add configuration options to the schema to make all of paths mandatory.
Require the username to be at least 5 characters long and the password to be at least 8 characters long.
NEW: Use the npm validator module to validate the email address.
NEW: Add custom error messages for all validators.
NEW: Add global validators to the String SchemaType so that all strings stored in the database are trimmed and non-empty.
Modify the POST /user endpoint handler in src/users/routers/users.js so that it retrieves data from the request’s body, create a document using the User model, and saves the document in the MongoDB database.
NEW: Update the catch-block in the handler so that it sends a 400 status code and appropriate data for validation errors and a 500 status code for all other errors.
Start your API server.
Use Postman to test the endpoint. Test them with valid and invalid data.
Use Compass to verify documents are created in the database.
Push the changes to your API server to GitHub.