# @novice1/routing

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

Latest version **2.2.0** (published 2026-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @novice1/routing
pnpm add @novice1/routing
yarn add @novice1/routing
bun add @novice1/routing
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2026-11-19 |
| First published | 2020-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 4 |
| Unpacked size | 46.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | demingongo |
| Maintainers | demingongo |
| Keywords | routing, express, route, router |

## Links

- npm: https://www.npmjs.com/package/@novice1/routing
- Repository: https://github.com/kisiwu/novice-routing
- Homepage: https://github.com/kisiwu/novice-routing#readme
- Issues: https://github.com/kisiwu/novice-routing/issues
- npm.io page: https://npm.io/package/@novice1/routing

## Dependencies (4)

- [router](https://npm.io/package/router.md) ^2.2.0
- [express](https://npm.io/package/express.md) ^5.3.0
- [methods](https://npm.io/package/methods.md) ^1.1.2
- [array-flatten](https://npm.io/package/array-flatten.md) ^3.0.0

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 2.2.0 (latest) — 2026-11-19
- 2.1.3 — 2026-10-24
- 2.1.2 — 2026-10-23
- 2.1.1 — 2026-05-24
- 2.1.0 — 2026-01-28
- 2.0.2 — 2025-05-05
- 2.0.1 — 2025-05-01
- 2.0.0 — 2025-04-23
- 1.1.7 — 2024-10-10
- 1.1.6 — 2024-09-21
- 1.1.5 — 2024-04-01
- 1.1.4 — 2023-08-13
- 1.1.3 — 2022-03-09
- 1.1.1 — 2021-08-15
- 1.1.0 — 2021-08-15
- … 12 more at https://npm.io/package/@novice1/routing/versions

## README

# @novice1/routing

A small extension of [Express (v5) routing](https://expressjs.com/en/guide/routing.html).

## Installation

```bash
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:

```js
import routing from '@novice1/routing'

routing().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`.


```js
import routing from '@novice1/routing'

const router = 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.

```js
import routing from '@novice1/routing'

const router = 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 `use`d by a router also having `auth` handlers. For that purpose there are some methods:

- `setAuthHandlersIfNone`
- `setValidatorsIfNone`

Example:

```js
import routing from '@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](https://www.npmjs.com/package/express) so you can always extend it more depending on your needs.

Example:
```ts
declare global {
  namespace Express {
    interface Request {
      // add a property
      session?: Record<string, any>;
    }
  }
}
```

## References

- [Express](https://expressjs.com/)

---
_Source: https://npm.io/package/@novice1/routing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
