# restroute

> Simple HTTP request/response router for simple REST applications.

Latest version **0.0.2** (published 2014-02-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install restroute
pnpm add restroute
yarn add restroute
bun add restroute
```

## 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.0.2 |
| Published | 2014-02-26 |
| First published | 2014-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Aviv Beeri |
| Maintainers | avivbeeri |
| Keywords | rest, router |

## Links

- npm: https://www.npmjs.com/package/restroute
- Repository: https://github.com/avivbeeri/node-restroute
- Issues: https://github.com/avivbeeri/node-restroute/issues
- npm.io page: https://npm.io/package/restroute

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

- 0.0.2 (latest) — 2014-02-26
- 0.0.1 — 2014-02-25

## README

RestRoute
==============

Simple HTTP request/response router for simple REST applications.

Setup
==============

Install RestRoute via npm:

    npm install restroute

and import it into your project a-like so:

    var router = require('restroute');

Usage
===============

RestRoute supports routes for all current HTTP/1.1 methods. To add a GET method handler looks like this:

    var pattern = ...
    router.get(pattern, handler);
    
    function handler (request, response) {
      //do things here
    });

Handlers for a given method are processed in the order they are added, so make sure to put the most specific handlers first, then add the general ones.

To execute the handler, simply pass your HTTP request and response objects through the `go(request, response)` method.

Handler Patterns
---------------

The `pattern` is a simple way to handle incoming urls from requests. It supports the following formatting features.

 * Strict text matching eg: set your pattern to `"/"` or `"/downloads"` or `"/images/cats"`
   * This is for exact matches, so `"/downloads"` would not match `"/downloads/sub-folder"`.
 * Parameterized matching: `"/:parameter"` or `"/testing/:parameterNameHere/data"`
   * These are made available as `request.params.parameterNameHere` for your callback function.
 * Wildcards for directory matching:
   * Loose: `pattern = "/downloads*"` means that `pattern` would match both `"/downloads"` and any subdirectory below it.
   * Strict: `pattern = "/downloads/*"` would only match the subdirectories, but not `"/downloads"`


Other Features
---------------
You can also provide a default error handler in the event that the request matches none of the current handlers.

    router.onError(errorHandler);
    function errorHandler(request, response) {
      //Error handling code here.
    }

Example
===============

This simple example demonstrates a simple use of RestRoute.

    //Imports
    var http = require('http');
    var router = require('restroute');

    //Handler functions
    function welcome (req, res) {
      res.writeHead(200);
      res.end("Hello world!");
    }
    function echo (req, res) {
      res.writeHead(200);
      res.end(req.params.echo);
    }
    function errors (req, res) {
      res.writeHead(404);
      res.end("404");
    }

    //Add handlers to the router
    router.onError(errors);
    router.get("/", welcome);
    router.get("/:echo", echo);

    //Use them in a server
    http.createServer(router.go).listen(8080);

Navigate to:

 * http://localhost:8080,, you should see "Hello World".
 * http://localhost:8080/something, you will recieve "something". 
 * But, http://localhost:8080/something/else results in "404" because of RestRoute's strict pattern matching. You could change this using the pattern `/:echo*` or `/:echo/*` depending on your needs.

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