This is a boilerplate for starting a Node.js RESTful API project.
Want to contribute?
- Install Node.js from nodejs.org.
- Just clone the repository using
git clone https://github.com/fernandominguez/nodejs-rest-api-boilerplate.git
- Run
npm install
in your favourite shell.
- For running unit tests on your local machine, just run
docker-compose up --build
. - For debugging on your local machine, just run
docker-compose -f docker-compose.dev.yml up --build
This boilerplate is ready to be tested with the Github Actions workflows.
You only have to do a git push
to your main
branch and you will see the result in this file header banners.
There are two workflows:
- API Tests: Performs the CI jobs and test if the changes fullfill the existing unit tests.
- CodeQL: Checks the repository's source code to find security vulnerabilities related to the coding language used (JavaScript).
The access to the API is done through API keys. To create an API key you have to create first a JWT private word to sign them.
This API expects the API key to be included in all API request to the server in a header that looks like the following:
Authorization: Bearer API_KEY
HTTP Request:
GET https://api.the-api.url/v1/auth
Call example to test your API key:
curl "https://api.the-api.url/v1/auth"
-H "Authorization: Bearer API_KEY"
Returns a JSON array like this:
{
"message": "Authorized",
"project": "Project Name"
}
To check an account status at any time you can use this endpoint.
It will respond with the user quota and current usage levels.
HTTP Request:
GET https://api.the-api.url/v1/account
Call example to test your API key:
curl "https://api.the-api.url/v1/account"
-H "Authorization: Bearer API_KEY"
Returns a JSON array like this:
{
"creation_date": "2021-01-31T21:00:08.452Z",
"uid": "oinenBADDnnelADDj83CAC98704R",
"paid_plan_name": "API Basic",
"image_api_quota": 1000,
"image_api_usage": 345,
"current_project": {
"name": "Project Name"
}
}
- Server: NodeJS
- Database: MongoDB
- NodeJS dependencies:
- bcryptjs (for token creation)
- body-parser (for parsing incoming request)
- compression (for compressing the response bodies)
- cors (for securitizing the API requests)
- crypto (for secret and token creation)
- dotenv-flow (for managing .env files)
- express (web framework for the server)
- fs (for file manipulation)
- helmet (for securitizing the API requests)
- http (for creating the HTTP API server)
- jsonwebtoken (for JWT token authentication)
- mongoose (for MongoDB connection)
- nodemon (for restarting the server on changes)