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

@novice1/routing

v1.1.5

Published

A small extension of [Express routing](https://expressjs.com/en/guide/routing.html).

Downloads

49

Readme

@novice1/routing

A small extension of Express routing.

Installation

$ npm install @novice1/routing

Usage

It keeps all the functionalities of Express router and extends them.

Router

A JSON object can be sent as the path parameter when using route methods (get, post, ...), all and route. That object must have the property path and can also have the following properties:

  • name: (string)
  • description: (string)
  • parameters: (object)
  • responses: (any)
  • tags: (string[])
  • auth: (boolean)
  • preValidators: (function[])

Example:

var router = require('@novice1/routing')()

router.get({
  path: '/',
  name: 'Home',
  description: 'Home page',
  parameters: {
    // ...
  },
  responses: {
    // ...
  },
  tags: ['Index']
}, function (req, res) {
  // information about the current route
  // can be found at req.meta
  res.json(req.meta)
})

Auth

From those properties, only path and auth influence the routing. When you need to verify the client's authentication the same way for a router's route, you can set middlewares with the method setAuthHandlers. Those middlewares will only be executed for routes with auth set to true.

var router = require('@novice1/routing')()

// set middleware(s) to handle authentication
router.setAuthHandlers(function (req, res, next) {
  // do something
  next()
}, function (req, res, next) {
  // do something else
  next()
})

router.get({
  name: 'Home',
  path: '/'
}, function (req, res) {
  res.send('hello world')
})

router.get({
  auth: true, // handle the authentication for this route
  name: 'Management',
  path: '/admin'
}, function (req, res) {
  res.send('hello admin')
})

setAuthHandlers can be called before or after creating the routes.

Validators

You can use setValidators to set handlers that valid the client's request. Those middlewares have access to req.meta so you could make use of the property parameters for example.

var router = require('@novice1/routing')()

router.get({
  name: 'Main app',
  // set parameters
  parameters: {
    query: {
      version: "number" // e.g.: the type that the query variable 'version' should have 
    }
  },
  path: '/app'
}, function (req, res) {
  res.json(req.meta)
})

// check requests for this router
router.setValidators(function (req, res, next) {
  if(req.meta.parameters.query.version == 'number') {
    if(!isNaN(req.query.version)) {
      // ok
      next()
    } else {
      // client's request is not valid
      res.status(400).send('Bad request')
    }
  } else {
    // ok
    next()
  }
})

setValidators can be called before or after creating the routes.

Other methods

As a router can be a middleware of another router (use method), you might want to keep different auth and validator handlers for some routers. For example a router might have its own auth handlers while being used by a router also having auth handlers. For that purpose there are some methods:

  • setAuthHandlersIfNone
  • setValidatorsIfNone

Example:

var routing = require('@novice1/routing')

var routerChild = routing()

routerChild.get('/', function (req, res) {
  res.json(req.meta)
})

routerChild.setValidators(function (req, res, next) {
  next()
})

var routerParent = routing()

routerParent.put('/', function (req, res) {
  res.json(req.meta)
})

// use 'routerChild' in 'routerParent'
routerParent.use(routerChild)

// set validators for routes except for 
// those already having validators
routerParent.setValidatorsIfNone(function (req, res, next) {
  next()
})

Notes

Typescript

This package extends Request interface from express so you can always extend it more depending on your needs.

Example:

declare global {
  namespace Express {
    interface Request {
      // add a property
      session?: Record<string, any>;
    }
  }
}

References