Node.js Library for building microservices communicating via Kafka
$ npm i mimimicrokafka
import { Gateway, Microservice } from 'mimimicrokafka';
options
<Object>
const gateway = new Gateway({
name: 'gateway',
microservices: ['orders'],
brokers: ['localhost:9092'],
});
app.use(gateway.middleware());
After initializat middleware, you can redirect requests from gateway to any microservice
app.use('/orders', async (req, res) => res.delegate('orders'));
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
app.use(async (req, res, next) => {
const data = await gateway.ask('jwt').post({path: '/jwt/check'}, req);
req.user = data.req.user;
next();
});
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...'));
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();
- 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"