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-http

v1.3.0

Published

HTTP kubik for Rubik

Downloads

6

Readme

rubik-http

HTTP Kubik for Rubik

Install

npm

npm i rubik-http

yarn

yarn add rubik-http

Use

const { App, Kubiks } = require('rubik-main');
const HTTP = require('rubik-http');
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();
// first param is a directory with routes or middlewares
// route: module.exports = { name: '/files', router: express.Router() };
// middleware: module.exports = function(req, res, next) { next() };
const http = new HTTP(path.join(__dirname, './routes/'));
// use is extension method for Kubik instances
http.use(async function(req, res, next) {
  req.user = await User.find(req.query.token);
  next();
});
http.use({
  middlewares: [
    (req, res, next) => {
      if (!req.user) {
        return next(new HTTP.Errors.HttpError(404, 'User not found'));
      }
      res.json(req.user);
    },
    { name: '/profiles', router: require('./profileRouter') }
  ]
});


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 or servers field, or both.

For example: config/http.js

module.exports = {
  // http will create http server and will listen 1993 port
  port: 1993,
  bind: 'localhost',
  // http will create 3 http servers with 1994, 1995, 1996 ports
  // localhost:1994, 1.1.1.1:1995, 0.0.0.0:1996
  servers: [
    { port: 1994 },
    { port: 1995, bind: '1.1.1.1' },
    { port: 1996, bind: 0 }
  ]
};

Extensions

When you add an instance of HTTP to app, you can use standart extensions interface, with other extensions

app.use({
  config: {
    volumes: [
      path.join(__dirname, './config'),
      path.join(__dirname, '../config')
    ]
  },
  http: {
    middlewares: [
      require('./whitelist.js'),
      require('./cacheControl.js')
    ]
  }
});

Also you can use use directly

http.use({
  middlewares: [
    require('./whitelist.js'),
    require('./cacheControl.js')
  ]
});

HTTP's instance has the following extensions

  1. function — just add as middleware
app.use({
  http: function(req, res, next) {
    console.info('Request starts at', new Date());
    next();
  }
});

http.use(async function(req, res) {
  const users = await User.findAll();
  res.json({ list: users });
  console.info('Request ends at', new Date());
});
  1. middlewares — array of middlewares
  2. routes volumes — directories with .js modules: module.exports = { name: '/files', router: express.Router() };
app.use({
  http: {
    volumes: [
      path.join(__dirname, './routes'),
      path.join(__dirname, '../filesRoutes')
    ]
  }
});
  1. before and after hooks
app.use({
  http: {
    before(http) {
      // before all middlewares, routes or other
    },
    after(http) {
      // after all middlewares, routes or other, but before create servers and listen
    }
  }
})

API

HTTP.API kubik add to HTTP /api route. It is completely the same as a HTTP, with their hooks, extensions and volumes of route's.

Add only one extension — apiResponseExtension

app.use({
  api: {
    apiResponseExtension: {
      text: 'Hi! This is api!'
    }
  }
});
// HTTP GET /api
// { api: 'ok', extension: { text: 'Hi! This is api!' } }

You need add HTTP.API kubik after HTTP kubik:

// ...
const api = new HTTP.API();
app.add([http, api]);
// or:
// app.add(http);
// app.add(api);
// ...