This project is a basic express setup with endpoints for user management and testing.
- User creation and retrieval endpoints
- Test endpoints
- Error handling middleware
- Connection to MongoDB database
- Unit tests with Jest
These instructions will get you a copy of this project up and running on your local machine.
- Node.js
- MongoDB
- Docker
- Clone the repository
- Install dependencies with
npm installoryarn install - Copy
.env.exampleto.envor fill in your MongoDB connection details and desired port
-
Make sure that Docker is running
-
To start the server, run
npm startoryarn start -
To start the development server, run
npm devoryarn dev
- To execute tests, run
npm testoryarn test
- POST /users: Accepts a user and stores it in the database, it should have a name and an email address.
- GET /users: Returns all users from the database. This endpoint can receive a query parameter
created, which sorts users by creation date ascending or descending
- GET /test: returns a
testmessage. - Get /test-error: Throws an error for testing error handling.
To create a user, make a POST request to the /users endpoint with a JSON body containing the user's name and email address:
curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe", "email":"john@example.com}' http://localhost:3111/usersTo retrieve all users make a GET request to the /users endpoint:
curl -X GET http://localhost:3111/usersYou can also sort users by creation date by adding a created query parameter (accepts ascending and descending). For example:
curl -X GET http://localhost:3111/users?created=descending