microa
v1.0.2
Published
Framework for easily building responsive microservices. 🔬
Downloads
1
Readme
microa
Framework for easily building responsive microservices. 🔬
Introduction
Framework works only with Node >= 8.5 and flag --experimental-modules
.
Install
$ npm i microa -S
Tests
$ npm test
Usage
Microservice natively can accept requests via http & sockets.
Backend:
import { createApp, createRoute } from 'microa';
createRoute('/users', {
'/create': (ctx) => {
// Backend magic... 🧙
ctx.body = { id: 1 };
},
});
createApp({
port: 3000,
debug: true,
});
Frontend:
// Create connect to the microservice
const socket = io('http://localhost:3000');
// Send an event via promisified emit method
const response = await socket.emitAsync('users:create', {
fistName: 'Mikhail',
lastName: 'Semin',
});
// Send an event via axios
const { data } = await axios.post('http://localhost:3000/users/create', {
fistName: 'Mikhail',
lastName: 'Semin',
});
Methods
Import all needed methods.
import { createModels, createRoute, createApp } from 'microa';
.createModels(models)
Create models for context. Every class constuctor accepts one argument ctx
.
createModels({
user: class User {},
balance: class Balance {},
});
After initializing models, instances will passed in ctx.models
.
createRoute('/users', {
'/me': (ctx) => {
// ctx.models.user
// ctx.models.balance
},
});
.createRoute(prefix, routes)
Create routes. Paths will transform for socket.io
& http
automatically. Be careful, http
requests work only via POST
method.
socket.io
⇒books:get-all
http
⇒/books/get/all
createRoute('/books', {
'/get/all': (ctx) => {
// ...
},
});
.createApp(options)
Start listening app.
createApp({
port: 8080,
debug: true,
});
License
MIT.