Building a RESTful API can seem daunting, but with the right tools and structure, it becomes a breeze! In this blog, we will explore how to set up a ready-to-use API project structure using Node.js, Express, and MongoDB. We’ll guide you through the installation process, explain the code using an easy analogy, and highlight troubleshooting tips along the way.
Getting Started
This basic API skeleton is crafted in JavaScript ES2015 and aims to make it exceptionally easy for beginners to build RESTful web APIs for front-end platforms like Android, iOS, or JavaScript frameworks (Angular, React, etc.).
Features
- Basic Authentication (Register/Login with hashed passwords)
- Account confirmation with a 4-digit OTP
- Email helper ready for direct import and use
- JWT Tokens for secure requests after login
- Pre-defined response structures with appropriate status codes
- Included CORS for cross-origin requests
- CRUD operations example with Books
- Validations included
- API collection available for Postman testing
- Test cases utilizing Mocha and Chai
- Code coverage reports using Istanbul.js
- Continuous Integration setup with Travis CI
- Linting with ESlint
Software Requirements
- Node.js version 8 or higher
- MongoDB version 3.6 or higher (recommended 4 or above)
How to Install
Using Git (Recommended)
- Clone the project from GitHub. Replace ‘myproject’ with your project name.
git clone https://github.com/maitraysuthar/rest-api-nodejs-mongodb.git myproject
Using Manual Download
- Download the repository as a ZIP file.
- Uncompress it to your desired directory.
Install NPM Dependencies
cd myproject
npm install
Setting Up Environments
- Locate the .env.example file in the root directory of the project.
- Copy and rename it to .env using the command:
cp .env.example .env
- Modify the values in the .env file to match your environment settings.
Project Structure Overview
To understand the project structure, think of building a house:
- app.js: The blueprint of your house, defining how everything comes together.
- controllers: Like room decorators, they handle the specific logic for different types of information (e.g., authentication, books).
- models: These are the foundations, holding the structure of your data (users, books).
- routes: The hallways connecting different rooms, guiding traffic to where it needs to go.
- middlewares: The filters that ensure everything flows smoothly along the hallways.
- test: The quality inspectors making sure everything is built correctly.
How to Run
To start your API server locally, you can execute the following command:
npm run dev
You will know your server is up and running when you see:
Connected to mongodb: YOUR_DB_CONNECTION_STRING
App is running ...
Creating New Features
If you wish to add new models, routes, or controllers, all you need to do is follow these simple steps:
- To add a new model, create a new file in the models directory.
- For new routes, create a file in the routes directory and add it to the api.js file.
- For new controllers, create a file in the controllers directory and link it to the routes.
Testing the API
To run the test cases, just type:
npm test
You can set custom commands for testing inside the package.json file.
Linting Your Code
Maintain code quality by running the linter:
npm run lint
Troubleshooting
If you encounter any issues during setup or execution, consider the following:
- Double-check your MongoDB connection string in the .env file.
- Ensure that Node.js and MongoDB meet the required versions.
- Look at the console for any error messages; they often give a clue on what to fix.
- Consult the Mocha and Chai documentation for testing issues.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
This guide covers the essentials of setting up a Node.js API using Express and MongoDB. As you dive into the world of API development, don’t hesitate to experiment and make this project your own. The beauty of coding is that there’s always something new to learn!
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.