A simple RESTful API built with Node.js, Express, and MongoDB, featuring image uploads.
By exploring this project, you will gain experience with:
- Express.js for building APIs efficiently.
- Mongoose for managing MongoDB interactions.
- Multer for handling image uploads.
- File system operations for deleting images when needed.
- Express-validator for request validation.
- Modularization to keep code maintainable.
- RESTful API principles for proper API design.
Clone the repository and install the necessary dependencies:
npm install
Create a .env at the root of your project and add the following configurations:
MONGODB_URL=mongodb+srv://<username>:<password>@cluster0.eox3j.mongodb.net/player-db?retryWrites=true&w=majority
PORT=3000
Replace username and password with your actual MongoDB Atlas credentials.
MONGODB_URL=mongodb://localhost:27017/player-db
PORT=3000
Choose the appropriate configuration based on your environment
Start the API with the following command:
node app.js
Your API will now be running on http://localhost:3000
This is an API that manages players (football for example). A player is characterized by:
- name: the name of the player
- age: the player's age
- bio: a brief biography of the player (at least 15 characters)
- photoUrl: player photo
- createdAt and updatedAt are generated automatically using the timestamps: true statement in the models/player.js file
So we have a total of 5 routes
- Add player http://localhost:3000/api/v1/player
- List of all players http://localhost:3000/api/v1/players
- Find player by Id http://localhost:3000/api/v1/player/:playerId
- Update player http://localhost:3000/api/v1/player/:playerId
- Delete player http://localhost:3000/api/v1/player/:playerId
- Player images are stored in the /images directory (ensure this folder exists at the same level as controllers/).
- When updating a player, the old image is automatically deleted if replaced.
- Reusable utility functions can be found in utils/syd-functions.js.
- Add Player
- List of players
For a complete API reference and to test the endpoints, visit the Postman documentation: https://documenter.getpostman.com/view/16446301/2sAYXBEeV5#c7c23cc9-2dbd-499e-abde-d443127a317b
This project is licensed under the MIT License. Feel free to use and modify it as needed!
Peace!