This is a dummy but extended example of a NodeJS microservice that can be used as a base for creating new one, more reliable and complete. It was created as resulting from my study about microservices which I did from my short experience creating NodeJS APIs and reading some good tutorials listed bellow.
I hope it can help you learn about microservice and enhance this base.
Considere the microServiceBaseConsumer for a complete example.
This microservices acts like and API entry within three endpoints:
/distance/:zipcode1/:zipcode2 the user inform two US zipcodes, the microservice fetch the distance between them from https://zipcodeapi.com/API, save it on MongoDB and sends a query log to RabbiMB, which will be consumed by our other microservice.
/distances fetchs and returns all the distances queries stored on our MongoDB.
/about show descritpion data from package.json
- api_routes: contains the routes managed by the microservice API
- config: contains the configuration files with the parameters used by the microservice (rename de default.json.template to default.json and fullfill it with your own parmeters)
- controllers: contains all the controllers which handles the requests, processes their data and delivers their responses
- db: contains the MongoDB connection object
- models: contains all the models responsible for manipulating the data (documents) from/to MongoDB
- services: contains serives that interacts with the external world (zipCodeAPI, fi) and other tools (RabbitMQ, fi)
- utils: contains other general purpose utilities like logger object
- have a MongoDB instance running
- have a RabbitMQ instance running
- get your on zipCodeAPI KEY from https://zipcodeapi.com/API
- rename config/defaul.json.template to config/default.json
- fullfill the config/defaul.json with you own parameters
- install the packages
npm i
- run
npm start
- access the endpoints :-)
For a better understanding, run the microServiceBaseConsumer in other terminal.
I'm thankfull for the these guys who helped me to learn about NodeJS microservices, MongoDB and RabbitMQ: