A yeoman generator for nodejs micro services with TypeScript, Express, Mongoose, Redis and RabbitMQ.
This project is a boilerplate for extensible micro services written in TypeScript. Contains the minimum to instant deploy a service on a Kubernetes Cluster.
This template contains:
- Kubernetes deployment configuration (including
- TypeScript definition for mongo operators on controller functions
MongoServiceabstract class for all entities services
GenericExceptionbase for all exceptions
withExceptiondecorator to abstract error handling logic (used on generated controllers)
RemoteControllerclass to handle
RabbitMQconsumers and producers logic
npm i -g yo
npm i -g generator-kube-microservice-node
- Follow the inscructions
How to run
yarn devto spawn a nodemon server watching source files
- Create a .env file in root to handle all your secrets. Look at
src/config/env.tsto see the default list of variables
Controllers of this boilerplate are handled by
Here is a exemple:
There's two types of response when using
- A result using
- The raw entity
The two examples are described above.
Everything is injected by
inversify and the composition root lives in
src/config/inversify.config.ts. Your entities controllers should be imported on
inversify-express-utils can inject your controller on express routes.
Inside the composition root, we import all controllers and
inversifyjs takes care to setup our application (as seen on
The service layer extends the
MongoService<T> which has all methods to handle the mongoose model.
Redis connection occurs when you require redis into another class. Use like this:
To use a consume/producer function for RabbitMQ, bootstrap the connection on your
Service like this:
The producer is straight forward: just call the function that sends something to a queue (ex:
All exceptions that are catch by
GenericException as they base.
So, just continuing throw new errors based on
GenericException.ts that express will catch and handle. (see
src/shared/exceptions/ folder for default exceptions created)
src/server/ you can find a
Unauthorized.ts file that handles authorization logic of this service.
Using this middleware, you should have another service with endpoint
/auth that receives a
If that service responds with 200, you're authorized to procced with your request into this service.
To use it, just insert into
src/server/ServerFactory.ts a line containing this middleware
This template uses
inversifyjs to handle DI with a IoC container.
The file that handles that is
If your controller has another class dependency, inject the dependency onto your class like this:
Docker and Kubernetes
To build a docker image, you have to build the project using
npm run build and
npm run build:webpack. Then, use
npm run build:docker, and to publish, use
npm run publish:docker. Remember to edit these commands if you use private repos.
The Kubernetes deployment file (
deployment.yaml), has a
LivenessProbe that checks if the route
/health returns 200. This route, pings to the database. If something goes wrong, your service will be restarted.
Service object in
deployment.yaml file expose the
Pod created by the
Deployment to the world on port 80 and binding the port 3000 of the
Pod to it.
After configuring, you need to add the
Service definition in a
ingress controller of your k8s cluster.
Since this template uses Kubernetes, the
Dockerfile files DOESN'T have a reference to
.envfile (which, also is ignored on
.gitignore file). The way to go about it is setting a
envFrom field on
Here is a example:
apiVersion: apps/v1kind: Deploymentmetadata:name: user-servicespec:replicas: 4selector:matchLabels:app: user-servicetemplate:metadata:labels:app: user-servicespec:containers:- name: user-serviceimage: <some-image>ports:- containerPort: 3000envFrom:- configMapRef:name: env-configlivenessProbe:initialDelaySeconds: 20periodSeconds: 5httpGet:path: /healthport: 3000
PR's and new issues are welcome. Anything you think that'll be great to this project will be discussed.
Clone this repo, then,
npm install and
npm link. Now you can test this generator locally using
Many thanks for the folks that worked hard on:
Without these libs, this boilerplate doesn't exists
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!