An example of REST API infrastructure which uses Node, Express and MongoDB.
Test the API with Postman easily.
Install docker
and docker-compose
in your machine to ability run everything.
# clone the project
$ git clone https://github.com/ridvanaltun/node-restful-api.git
# navigate to the project folder
$ cd node-restful-api
# install dependencies
$ npm install
# create global volume for MongoDB
$ docker volume create --name=node-app-database-data
# create global volume for Redis
$ docker volume create --name=node-app-redis-data
# create your own environment file for development
$ cp .env.example .env
# create your own environment file for production
$ cp .env.example .env.production
Don't forget to customize your environment file.
Remember, refresh token and access token can not be same.
# generate jwt secrets
$ node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
JWT_REFRESH_TOKEN_SECRET
JWT_ACCESS_TOKEN_SECRET
You can create a fake SMTP server using Mailtrap for development purposes. With Mailtrap all emails automatically forward to your Mailtrap inbox.
EMAIL_HOST
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD
EMAIL_PORT
Activation code will send using this email address. Example no-reply@domain.com
.
EMAIL_ADDRESS_NO_REPLY
These configuration variables are using by activation email.
FRONTEND_ADDRESS
FRONTEND_EMAIL_VERIFICATION_PATH
FRONTEND_RESET_PASSWORD_PATH
FRONTEND_LOGO_URL
You can start the API server for development or production.
If you wanna move on developing this, go ahead with below command:
# start everything for development environment
$ docker-compose up -d
After all's below services will work:
API Server
- runs at 3000 (It is a Node.js app)MongoDB
runs at 27017Mogoku
runs at 3100 (MongoDB Collection Viewer)Redis
runs at 6379Redis Commander
runs at 8081 (Redis Web Management Tool)
Note: All changes on API will effects immediately.
Or you can build up a production environment follow the below command:
# start everything for production environment
$ docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
Below services will start:
API Server
- runs at 3000MongoDB
runs at 27017Redis
runs at 6379
Note: Changes on API will not effects production.
API versions are coming from the environment file. For using multiple version of API, use a version control system like git and tag every version on it. After all, you can use tags to deploy spesific version of the API. Version controlling of API on code is possible but it is bit complex. I think this way covers the best practices.
- Add JWT (JSON Web Tokens)
- Add ability to refresh JWT token
- Add ability to blacklist JWT token on logout
- Add API documentation (Swagger) generator
- Add Postman collection
- Add API limitter for security
- Add Joi and Celebrate support for API validation
- Add password hashing
- Add cron jobs manager (Agenda)
- Add API versioning
- Add pagination
- Run server with environment (dev, prod)
- Add login and logout logs
- Add request logger
- Add documantation for pagination and endpoints
- Add access control system
- Add auto semantic release with a CI/CD tool
- Dockerize everything
- Deploy API for showcase
- Debugger support for NodeJS app in Docker