# map-router

> Fast exact match router for Express.js

Latest version **1.0.0** (published 2015-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install map-router
pnpm add map-router
yarn add map-router
bun add map-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-02-25 |
| First published | 2015-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.12 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Andris Reinman |
| Maintainers | andris |
| Keywords | express, router |

## Links

- npm: https://www.npmjs.com/package/map-router
- Repository: https://github.com/andris9/map-router
- Issues: https://github.com/andris9/map-router/issues
- npm.io page: https://npm.io/package/map-router

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

- 1.0.0 (latest) — 2015-02-25

## README

# MapRouter

**MapRouter** is a simple and fast *exact-match* router middleware for Express that can be used instead of the built-in router.

Requires Node v0.12 or iojs. The module does not run on Node v0.10 as it uses [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) constructor which is not enabled by default (you might get it working with the `--harmony` flag though).

The goals of this router are:

  * Should be very fast
  * Routes can be added dynamically
  * Routes can be removed dynamically

The sacrifices for achieving the goals are:

  * Only exact matches, no regular expressions or variable support

This router is suitable for scenarios where you need to add and remove routes very often in runtime.

## Usage

Install with npm

    npm install map-router

Require in your script

    var MapRouter = require('map-router');

Create MapRouter instance

```javascript
var router = new MapRouter();
```

Attach the router to Express

```javascript
app.use(router.middleware());
```

### Add a new Route

```javascript
router.register(method, path, handler)
```

Where

  * **method** is the method to use or `'ALL'` for matching all methods
  * **path** is the path to the route (eg. `'/path/to/handler'`)
  * **handler** is the route handler

**Example**

```javascript
var express = require('express');
var app = express();
var MapRouter = require('map-router');
var router = new MapRouter();

app.use(router.middleware());

router.register('GET', '/', function(req, res){
    res.send('hello world');
});
```

Alternatively you can register handlers with shorthand wrappers where method is the function name:

```javascript
router.get('/', function(req, res){
    res.send('hello world');
});
```

### Remove a Route

```javascript
router.unregister(method, path)
```

Where

  * **method** is the method to use or `'ALL'` for matching all methods
  * **path** is the path to the route (eg. `'/path/to/handler'`)

### Find a Route for a Path

If you are not using the Express compatible middleware but want to do your own routing, you can do this with

```javascript
var handler = router.route(method, path)
```

Where

  * **method** is the method to use or `'ALL'` for matching all methods
  * **path** is the path to the route (eg. `'/path/to/handler'`)
  * **handler** is the resolved handler function or false if the route was not found

## License

**MIT**

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