ridvanaltun / node-restful-api

An example of REST API infrastructure which uses Node, Express and MongoDB.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node RESTful API

semantic-release Commitizen friendly

An example of REST API infrastructure which uses Node, Express and MongoDB.

Postman Collection

Test the API with Postman easily.

Run in Postman

Table of Contents

Prerequisite

Install docker and docker-compose in your machine to ability run everything.

Installation

# 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

Customizing Environment File

Don't forget to customize your environment file.

JWT Secrets

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

E-Mail Server

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

E-Mail Addreses

Activation code will send using this email address. Example no-reply@domain.com.

EMAIL_ADDRESS_NO_REPLY

Activation E-Mail Related

These configuration variables are using by activation email.

FRONTEND_ADDRESS
FRONTEND_EMAIL_VERIFICATION_PATH
FRONTEND_RESET_PASSWORD_PATH
FRONTEND_LOGO_URL

Usage

You can start the API server for development or production.

Development

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 27017
  • Mogoku runs at 3100 (MongoDB Collection Viewer)
  • Redis runs at 6379
  • Redis Commander runs at 8081 (Redis Web Management Tool)

Note: All changes on API will effects immediately.

Production

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 3000
  • MongoDB runs at 27017
  • Redis runs at 6379

Note: Changes on API will not effects production.

API Versioning

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.

Checklist

  • 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

About

An example of REST API infrastructure which uses Node, Express and MongoDB.

License:MIT License


Languages

Language:JavaScript 93.8%Language:Shell 3.2%Language:Dockerfile 3.1%