Framework for easily building responsive microservices. π¬
Framework works only with Node >= 8.5 and flag --experimental-modules
.
$ npm i microa -S
$ npm test
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',
});
Import all needed methods.
import { createModels, createRoute, createApp } from 'microa';
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
},
});
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) => {
// ...
},
});
Start listening app.
createApp({
port: 8080,
debug: true,
});
MIT.