Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bifot/microa
🔬 Framework for easily building responsive microservice.
https://github.com/bifot/microa
Last synced: 5 days ago
JSON representation
🔬 Framework for easily building responsive microservice.
- Host: GitHub
- URL: https://github.com/bifot/microa
- Owner: bifot
- Created: 2018-09-08T08:09:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-01T14:57:47.000Z (almost 6 years ago)
- Last Synced: 2024-12-14T13:02:33.700Z (9 days ago)
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![microa](https://img.shields.io/npm/v/microa.svg?style=flat-square)](https://www.npmjs.com/package/microa)
![microa](https://img.shields.io/travis/com/bifot/microa.svg?style=flat-square)
![microa](https://img.shields.io/badge/code%20style-airbnb-brightgreen.svg?style=flat-square)# microa
Framework for easily building responsive microservices. 🔬
## Introduction
Framework works only with Node >= 8.5 and flag `--experimental-modules`.
## Install
```sh
$ npm i microa -S
```## Tests
```sh
$ npm test
```## Usage
Microservice natively can accept requests via http & sockets.
**Backend:**
```js
import { createApp, createRoute } from 'microa';createRoute('/users', {
'/create': (ctx) => {
// Backend magic... 🧙
ctx.body = { id: 1 };
},
});createApp({
port: 3000,
debug: true,
});
```**Frontend:**
```js
// 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',
});
``````js
// Send an event via axios
const { data } = await axios.post('http://localhost:3000/users/create', {
fistName: 'Mikhail',
lastName: 'Semin',
});
```## Methods
Import all needed methods.
```js
import { createModels, createRoute, createApp } from 'microa';
```### .createModels(models)
Create models for context. Every class constuctor accepts one argument `ctx`.
```js
createModels({
user: class User {},
balance: class Balance {},
});
```After initializing models, instances will passed in `ctx.models`.
```js
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````js
createRoute('/books', {
'/get/all': (ctx) => {
// ...
},
});
```### .createApp(options)
Start listening app.
```js
createApp({
port: 8080,
debug: true,
});
```## License
MIT.