In the last 5 years, I worked with web application development, most of them using Node.js and MongoDB.
I gathered here what I think is fundamental and a good starting point to develop REST APIs with Node.js and MongoDB.
The server includes:
- Authentication;
- Permissions;
- Data validation (URL parameters, query parameters and body);
- Basic CRUD easy to be replicated;
- Standardized errors;
- Standardized responses;
- E2E tests;
I consider the number of dependencies relatively low, and I think it is important to keep it this way, whenever possible.
It is necessary to have Node.js installed, preferably in its most recent version.
git clone https://github.com/conradoac/node-rest-api.git
npm install
Before running, you need to create a file named .env at the same level as the src folder. This file must contain two information: DB_URI
and SECRET_KEY
.
DB_URI=mongodb://localhost:27017/mydb
SECRET_KEY=SomeSecretKey
npm start
For tests to run correctly, two things are necessary:
-
A .env file in the test directory. This file must contain the same information as the application's .env file, but with the test environment settings.
-
A user registered in the database, with the following information:
{
"username": "tester",
"password": "123456",
"role": "Admin"
}
npm test