mimimicrokafka

Node.js Library for building microservices communicating via Kafka

Usage no npm install needed!

<script type="module">
  import mimimicrokafka from 'https://cdn.skypack.dev/mimimicrokafka';
</script>

README

mimiMicroKafka

Node.js Library for building microservices communicating via Kafka

npm package status version number License

Install

$ npm i mimimicrokafka

Usage

import { Gateway, Microservice } from 'mimimicrokafka';

API:

Gateway

constructor(options)

Use gateway

const gateway = new Gateway({
  name: 'gateway',
  microservices: ['orders'],
  brokers: ['localhost:9092'],
});

app.use(gateway.middleware());

res.delegate(nameMicroservice)

After initializat middleware, you can redirect requests from gateway to any microservice

app.use('/orders', async (req, res) => res.delegate('orders'));

microservice.ask(nameMicroservice)[metod](params, req)

Sends a request to another microservice from any service

  • microservice <Gateway | Microservice> Class of any microservice.
  • nameMicroservice <String> Name microservice which we are sending the request.
  • metod <Function> post | get | put | delete
    • params <Object> Here you can complete request.
    • req <Object> Raw req from parent request.
app.use(async (req, res, next) => {
  const data = await gateway.ask('jwt').post({path: '/jwt/check'}, req);
  req.user = data.req.user;
  next();
});

For example:

index.js

import express from 'express'
import bodyParser from 'body-parser'
import { Gateway } from 'mimimicrokafka'

const app = express();
app.use(bodyParser.json());

const gateway = new Gateway({
  name: 'gateway',
  microservices: ['orders'],
  brokers: ['localhost:9092'],
});

app.use(gateway.middleware());

app.use(async (req, res, next) => {
  const data = await gateway.ask('jwt').post({path: '/jwt/check'}, req);
  req.user = data.req.user;
  next();
});

app.use('/orders', async (req, res) => res.delegate('orders'));
app.use('/users', async (req, res) => res.delegate('users'));
app.use('/auth', async (req, res) => res.delegate('auth'));

app.listen(8080, () => console.log('listening 8080...'));

orders.js

import { Microservice } from 'mimimicrokafka'

const app = new Microservice({
  microservice: 'orders',
  brokers: ['localhost:9092'],
});

app.get('/orders', async (req, res) => res.send('req.body'));

app.post('/orders', async (req, res) => res.json(req.body));

app.post('/orders/123', async (req, res) => {
  const {data} = await app.ask('users').post({path: '/users', body: {
    data: req.body.data + 1,
  }}, req);
  console.log(req.user);
  return res.json(data);
});

app.start();

TODO:

  • add KafkaStreams capability
  • optimize the route for returning the answer to gateway
  • add the ability to raise your server through the "http" library without "express"

Pull request welcome ✌🏻