# connect-routing

> A routing middleware for connect

Latest version **0.1.1** (published 2015-11-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install connect-routing
pnpm add connect-routing
yarn add connect-routing
bun add connect-routing
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2015-11-08 |
| First published | 2015-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | eduardn |
| Maintainers | eduardn |

## Links

- npm: https://www.npmjs.com/package/connect-routing
- npm.io page: https://npm.io/package/connect-routing

## Dependencies (1)

- [url](https://npm.io/package/url.md) ^0.11.0

## Recent versions

- 0.1.1 (latest) — 2015-11-08
- 0.1.0 — 2015-11-07

## README

[![Build Status](https://travis-ci.org/eduardn/connect-routing.svg?branch=master)](https://travis-ci.org/eduardn/connect-routing)

# connect-routing
A router for connect written in ES6. This project aims to be a complete and fully featured router for connect.

# Docs
## Use with the provided middleware

```JavaScript
var connect = require('connect');
var Router = require('connect-routing');

var app = connect();
var router = Router.instance();

app.use(Router.middleware);

router.addRoute('/my/simple/route', function (req, res, next) {
    res.end('Hello, world!');
    next();
});
```

## Use with your own middleware

```JavaScript
var connect = require('connect');
var Router = require('connect-routing');

var app = connect();
var router = Router.instance();

app.use(function (req, res, next) {

    // .... Your custom code here ....

    // Parse the url and get the pathname
    // Try to call the route
    try {
        router.callRoute(path, req, res, next);
    } catch (e) {
        // If no route is found display a 404 to the user, or some error

        // .... Error code here ....
    }
});

router.addRoute('/my/simple/route', function (req, res, next) {
    res.end('Hello, world!');
    next();
});
```

## Route Parameters
Path parameters will be available in the ```req.params``` object. See the example below.

```JavaScript
var connect = require('connect');
var Router = require('connect-routing');

var app = connect();
var router = Router.instance();

app.use(Router.middleware);

router.addRoute('/users/:userId', function (req, res, next) {
    res.end('Found user with id ' + req.params.userId);
    next();
});
```

## Parameters regex matchers
Parameters can have custom regex matchers, for example you might want to accept
only numbers for a certain parameter. To do that you define your parameter with
a name followed by a regex in parenthesis (e.g. ```:id(([0-9]+))```).

```JavaScript
var connect = require('connect');
var Router = require('connect-routing');

var app = connect();
var router = Router.instance();

app.use(Router.middleware);

router.addRoute('/users/:id(([0-9]+))', function (req, res, next) {
    res.end('This route matched the number ' + req.params.id + ' for id parameter');
    next();
});
```

## Different HTTP Methods
To use a different http method for a route see the example below

```JavaScript
var connect = require('connect');
var Router = require('connect-routing');

var app = connect();
var router = Router.instance();

app.use(Router.middleware);

router.addRoute('POST', '/users', function (req, res, next) {
    res.end('Creating a new user...');
    next();
});
```

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