npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rubik-micro

v1.5.1

Published

Micro server kubik for the Rubik

Downloads

11

Readme

rubik-micro

Micro server kubik for the Rubik

Install

npm

npm i micro
npm i rubik-micro

yarn

yarn add micro
yarn add rubik-micro

Use

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

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

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

http.use([
  require('./whitelist.js'),
  require('./cacheControl.js')
]);

Micro's instance has the following extensions

  1. function — just add as middleware
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());
});
  1. Array of functions — add more then one middlewares
app.use({
  http: [
    require('./parseBody.js'),
    require('./whitelist.js')
  ]
});

http.use([
  require('./cacheControl.js'),
  require('./ruleControl.js')
]);
  1. Add custom catcher or listener
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');
  }
});
  1. before and after hooks
app.use({
  http: {
    before(micro) {
      // before apply any middleware
    },
    after(micro) {
      // after apply all middlewares, but before create server and listen
    }
  }
})
  1. middlewares and other extensions with one object
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
    }
  }
});
  1. Array composition of extensions
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
      }
    }
  ]
});