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

route-b

v0.2.0

Published

Library for routing by files and methods

Downloads

4

Readme

route-b

Route library/middleware for Node.js

Description

It makes possible to use your files (modules) and methods in it for routing.

Installation via npm

npm install route-b

Examples of usage situations

RestAPI by files and methods

You have such file structure: +api/ |-posts.js |-users.js Each file has 5 methods: get, post, patch, delete, all And you want that url GET /api/users call get() of users.js, POST /api/users call post() of users.js ... and so on

Route by files and it's methods

You have such file structure: +controllers/ |-posts.js |-users.js Each file has own list of methods like stats(), ready(), find() ... And you want that url /posts/stats call stats() of posts.js, /users/find call find() of users.js ... and so on

Usage

With Express.js

var exppress = require('express');
var app      = express();
var routeB   = require('routeB');
var path     = require('path');

// RestAPI example
app.use('/api', routeB({
  dir: path.resolve(__dirname, 'api'),
    map: {
      module : 'path[0]',
      method : 'method'
    },
    defaults: {
      module : null,
      method : 'all'
    },
    errors: {
      noModule: function (req, res) {
        res.status(404).send({error: 'No source for ' + req.routeB.callObj.moduleName});
      },
      noMethod: function (req, res) {
        res.status(405).send({error: 'Method not allowed ' + req.routeB.callObj.methodName});
      }
    }
}));

Options

{
  dir: '', // Dir where your files (modules) are
  map: { // Object for setting route algorithm. Values is string- or array-path of queryObj  
    module : 'path[0]', // How find fille (module)
    method : 'path[1]' // Which method to call
  },
  defaults: { // Default names of module and method
    module : '_default',
    method : 'default'
  },
  errors: { // Error handlers
    noModule : errorHandler,
    noMethod : errorHandler
  }
}

Map values - is string or array value, which can be represented as path to key of queryObj. Used get() method of lodash library: Examples:

  • path[0] mean value of queryObj.path[0]
  • method mean value of queryObj.method
  • query.users[2] mean value of queryObj.query.users[2]

route-b add to req object routeB. And you can access it from your custom error handlers functions or from your controllers. Because route-b forwards req, res, next to controllers and error handlers.

routeB = {
   queryObj: { // get from current query. /users/find?page=1 for example
     method : 'get', // Lowercase query method
     path   : ['users', 'find'], // Array of path parts
     query  : {page: 1} // Query object
   },
   callObj: { // There are module object, method function ant their names for current query
     moduleName: 'users', // Module name to require
     methodName: 'find', // Method name to call
     module: ..., // Module object
     method: ... // Method function
   }
}