/nodejs-microservice-example

A NodeJS microservice project"

Primary LanguageJavaScript

NodeJS Microservice example

Workflow status badge codecov Maintainability Made with love in Brazil

A comprehensive Node.js microservice template that provides a solid foundation for building and deploying production-ready microservices.

Speed up your development process with a ready-to-deploy microservice codebase.

Features

  • Containerization with Docker (multi-stage and secure)
  • Health check endpoint for container monitoring and management
  • Structured logging with JSON using Winston
  • Graceful server shutdown handlers in Express
  • Continuous Integration with Github Actions Workflows
  • RESTful API for a sample resource
  • Request payload validation using middleware
  • Data persistence with MongoDB
  • Unit testing with Jest and Supertest
  • Dependencies vulnerabilities scan with Snyk
  • Test coverage with CodeCov

Prerequisites

  • Node.js
  • Docker

Getting Started

Installation

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

  1. Clone the repository: git clone https://github.com/adriandantas/nodejs-microservice-example.git
  2. Install the dependencies: npm install

Configuration

The following environment variables can be used to configure the microservice:

  • PORT: the port number to run the microservice on. If this variable is not set, the microservice will run on port 3000 by default.
  • MONGO_URI: the URI of the MongoDB database to connect to. If this variable is not set, a instance of mongodb-memory-server will be used.
  • NODE_ENV: the environment in which the microservice is running. This variable can be set to production, staging, or development.

API

Endpoints

The following routes are available for the example REST resource:

GET     /api/healthcheck    # Container healthcheck endpoint
GET     /api/films          # List all resource
GET     /api/films/:id      # Find resource by id
POST    /api/films          # Create new resource
PUT     /api/films/:id      # Update an existing resource
DELETE  /api/films/:id      # Delete a resource

API testing with Postman and Newman

This project contains a Postman collection configured to http://localhost:3000/ that can be used test the API using Newman.

  npm run newman

You can create your own sandbox for testing simply by importing the collection into Postman.

Swagger documentation

In order generate an updated openapi.yaml file just execute the following npm task:

  npm run swagger

The updated documentation can be found in ./doc/api

Deployment

Cloud instant deployment

Google Cloud

Run on Google Cloud

Local Deployment

To build the Docker image, run the following command in the project directory:

docker build -t nodejs-microservice-example .

To run the Docker container, use the following command:

docker run -p 3000:3000 nodejs-microservice-example

Replace 3000 with the desired host port.

Built With

Author