moleculer
Version:
Fast & powerful microservices framework for Node.JS
129 lines (101 loc) • 5.6 kB
Markdown

[](https://travis-ci.org/ice-services/moleculer)
[](https://coveralls.io/github/ice-services/moleculer?branch=master)
[](https://www.codacy.com/app/mereg-norbert/moleculer?utm_source=github.com&utm_medium=referral&utm_content=ice-services/moleculer&utm_campaign=Badge_Grade)
[](https://codeclimate.com/github/ice-services/moleculer)
[](https://david-dm.org/ice-services/moleculer)
[](https://snyk.io/test/github/ice-services/moleculer)
[](https://gitter.im/ice-services/moleculer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://www.npmjs.com/package/moleculer)
[](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fice-services%2Fmoleculer?ref=badge_shield)
# Moleculer [](https://www.npmjs.com/package/moleculer)
Moleculer is a fast & powerful microservices framework for NodeJS (>= v6.x).
<!--




-->
**Website**: [https://moleculer.services](https://moleculer.services)
**Documentation**: [https://moleculer.services/docs](https://moleculer.services/docs)
# What's included
- Promise-based solution
- request-reply concept
- event bus system
- supports middlewares
- service mixins
- multiple services on a node/server
- built-in caching solution (memory, Redis)
- pluggable transporters (NATS, MQTT, Redis)
- pluggable serializers (JSON, Avro, MsgPack, Protocol Buffer)
- load balanced requests (round-robin, random)
- every nodes are equal, no master/leader node
- auto discovery services
- parameter validation with [fastest-validator](https://github.com/icebob/fastest-validator)
- distributed timeout handling with fallback response
- health monitoring, metrics & statistics
- supports versioned services (run different versions of the service)
- official [API gateway module](https://github.com/ice-services/moleculer-web)
# Installation
```
$ npm install moleculer --save
```
or
```
$ yarn add moleculer
```
# Create your first microservice
This example shows you how to create a small service with an `add` action which can add two numbers.
```js
const { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({ logger: console });
broker.createService({
name: "math",
actions: {
add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
}
}
});
broker.start();
// Call service
broker.call("math.add", { a: 5, b: 3 })
.then(res => console.log("5 + 3 =", res))
.catch(err => console.error(`Error occured! ${err.message}`));
```
[Try it on Runkit](https://runkit.com/icebob/moleculer-quick-example)
# Create a Moleculer project
Use the Moleculer CLI tool to create a new Moleculer based microservices project.
1. Install [moleculer-cli](https://github.com/ice-services/moleculer-cli) globally
```bash
$ npm install moleculer-cli -g
````
2. Create a new project (named `first-demo`)
```bash
$ moleculer init project-simple first-demo
````
> Add API Gateway and press Y to `npm install`
3. Open project folder
```bash
$ cd first-demo
````
4. Start project
```bash
$ npm run dev
````
5. Open the [http://localhost:3000/math.add?a=5&b=3](http://localhost:3000/math.add?a=5&b=3) link in your browser. It will call the `add` action of `math` service with two params via [API gateway](https://github.com/ice-services/moleculer-web) and returns with the result.
:tada:**Congratulations! You created your first Moleculer based microservices project. Read our [documentation](https://moleculer.services/docs) to learn more about Moleculer.**
# Official modules
We have some official modules for Moleculer. [See the list!](https://moleculer.services/docs/modules.html)
# Documentation
You can find here [the documentation](https://moleculer.services/docs).
# Changelog
See [CHANGELOG.md](CHANGELOG.md).
# Roadmap
See [ROADMAP.md](ROADMAP.md).
# License
Moleculer is available under the [MIT license](https://tldrlegal.com/license/mit-license).
[3rd party licenses](https://app.fossa.io/reports/833f0d1b-462b-4eff-a4e4-e030115439fe)
# Contact
Copyright (c) 2016-2017 Ice Services
[](https://github.com/ice-services) [](https://twitter.com/MoleculerJS)