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.
- 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
- Node.js
- Docker
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Clone the repository:
git clone https://github.com/adriandantas/nodejs-microservice-example.git
- Install the dependencies:
npm install
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 toproduction
,staging
, ordevelopment
.
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
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.
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
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.