- Straight forward
- Build your next nodejs microservice in seconds
- GRPC based
- nodejs microservice boilerplate is based on GRPC, A high performance, open-source universal RPC framework.
- ES6 Classes
- Develope your node.js microservice using the power of ES6 classes without the need of transpilers! So develop and debug your code without headaches
- MVC pattern
- Build you micro-service in a well-known MVC architecture.
Keywords: Node.js, GRPC, Micro Service, ES6
- Make sure that you have Node v8 or above installed.
- Clone this repo using
git clone https://github.com/omidnavy/nodejs-microservice-boilerplate.git
- Move to the appropriate directory:
cd nodejs-microservice-boilerplate
. - Run
npm install
in order to install dependencies . - Run
npm start
to run sample.
Now you're ready to rumble!
Please note that this boilerplate is production-ready!
-
First, let's make a Protocol Buffer name it
RouteMapper.proto
and put it inside/app/core/
. -
Open
/app/core/RouteMapperMapper.js
and editpackageName
andserviceName
variable depends on you.proto
file. -
For every rpc function defined in your proto, you must have a component.
- Create a folder inside
app/components
same as your rpc function name.eg. SayHello
- Now make a
[x]Controller.js
inside this folder wherex
is your component name. use/app/components/SayHello/SayHelloController.js
as your template. - You can declare model classes inside your component folder then require it in your controller class. Use
/app/components/SayHello/myModel.js
as your template. - There is a
BaseController
andBaseModel
class inside/app/core/
which can have your common things for controllers and models.
- Create a folder inside
-
Time to run your app:
npm run start
There is a simple BaseDBModel
in /app/core/
which is using mysql
pooled connections wrapped in a promised based function.
You can check the commented sample code for using it.
You can modify it to use with other databases or remove it completely from your app if you don't need databases . don't forget to clean the code and uninstall mysql
client using following command if you decide to remove or change database.
npm uninstall -s mysql
Helpers are shared functions that can be called from anywhere in your app. here we use global variables
to define our helper functions.
There is logger
helper function defined as sample , you can call it with logger()
where ever you want. there is a child process for calling logger function to reduce cpu load on main app (don't take it serious , its just a showcase).
If you want to use child process, you can use /app/core/child-process/winston.js
as your template.