Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ya-kostik/rubik-micro
Micro server kubik for the Rubik
https://github.com/ya-kostik/rubik-micro
http kubik micro nodejs rubik
Last synced: about 2 months ago
JSON representation
Micro server kubik for the Rubik
- Host: GitHub
- URL: https://github.com/ya-kostik/rubik-micro
- Owner: ya-kostik
- License: apache-2.0
- Created: 2018-06-27T11:03:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T03:20:05.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T04:41:37.617Z (9 months ago)
- Topics: http, kubik, micro, nodejs, rubik
- Language: JavaScript
- Size: 408 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rubik-micro
Micro server kubik for the Rubik## Install
### npm
```bash
npm i micro
npm i rubik-micro
```### yarn
```bash
yarn add micro
yarn add rubik-micro
```## Use
```javascript
const { App, Kubiks } = require('rubik-main');
const Micro = require('rubik-micro');
const path = require('path');// create rubik app
const app = new App();
// config need for most modules
const config = new Kubiks.Config(path.join(__dirname, './config/'));
// you can use any logger you want, just create kubik with it
// default Kubiks.Log use console for logging
const log = new Kubiks.Log();const micro = new Micro();
// use is extension method for Kubik instances
micro.use(async function(req, res/*, micro */) {
res.end('Ok, it is done');
});
// You can use any middlewares you want. If you want to stop process middlewares, just `return false` from middleware.app.add([ config, log, http ]);
app.up().
then(() => console.info('App started')).
catch(err => console.error(err));
```## Config
`http.js` config in configs volume should contain port, you can specify bind too.For example:
`config/http.js`
```javascript
module.exports = {
// http will create http server and will listen 1993 port
port: 1993,
bind: 'localhost'
};
```## Extensions
When you add an instance of Micro to app, you can use standart extensions interface,
with other extensions
```javascript
app.use({
config: {
volumes: [
path.join(__dirname, './config'),
path.join(__dirname, '../config')
]
},
http: [
require('./whitelist.js'),
require('./cacheControl.js')
]
});
```Also you can use `use` directly
```javascript
http.use([
require('./whitelist.js'),
require('./cacheControl.js')
]);
```Micro's instance has the following extensions
1. function — just add as middleware```javascript
app.use({
http: function(req, res, micro) {
console.info('Request starts at', new Date());
return true;
}
});http.use(async function(req, res) {
const users = await User.findAll();
res.json({ list: users });
console.info('Request ends at', new Date());
});
```
2. Array of functions — add more then one middlewares```javascript
app.use({
http: [
require('./parseBody.js'),
require('./whitelist.js')
]
});http.use([
require('./cacheControl.js'),
require('./ruleControl.js')
]);
```
3. Add custom catcher or listener```javascript
app.use({
http: {
catcher: (req, res, err) => {
console.error(err);
res.end('Internal server Error');
}
}
});http.use({
listener: (req, res) => {
res.end('200 is Ok');
}
});
```
4. before and after hooks```javascript
app.use({
http: {
before(micro) {
// before apply any middleware
},
after(micro) {
// after apply all middlewares, but before create server and listen
}
}
})
```5. middlewares and other extensions with one object
```javascript
app.use({
http: {
middlewares: [
require('./parseBody.js'),
require('./whitelist.js')
],
listener: (req, res) => {
res.end('200 is Ok');
},
catcher: (req, res, err) => {
console.error(err);
res.end('Internal server Error');
},
before(micro) {
// before apply any middleware
},
after(micro) {
// after apply all middlewares, but before create server and listen
}
}
});
```6. Array composition of extensions
```javascript
app.use({
http: [
require('./parseBody.js'),
require('./whitelist.js'),
require('./cacheControl.js'),
require('./ruleControl.js'),
{
listener: (req, res) => res.end('200 is Ok')
},
{
catcher: (req, res, err) => {
console.error(err);
res.end('Internal server Error');
}
},
{
before(micro) {
// before apply any middleware
},
after(micro) {
// after apply all middlewares, but before create server and listen
}
}
]
});
```