# httpdispatcher

> A simple class allows developer to have a clear dispatcher for dynamic pages and static resources.

Latest version **2.2.0** (published 2021-11-30) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2.2.0 |
| Published | 2021-11-30 |
| First published | 2012-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Alberto Bottarini |
| Maintainers | alberto-bottarini |
| Keywords | util, httpdispatcher, dispatcher |

## Links

- npm: https://www.npmjs.com/package/httpdispatcher
- Repository: https://github.com/alberto-bottarini/httpdispatcher
- Homepage: https://github.com/alberto-bottarini/httpdispatcher#readme
- Issues: https://github.com/alberto-bottarini/httpdispatcher/issues
- npm.io page: https://npm.io/package/httpdispatcher

## Dependencies (1)

- [mime-types](https://npm.io/package/mime-types.md) >=1.2.5

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 2.2.0 (latest) — 2021-11-30
- 2.1.2 — 2018-02-08
- 2.1.1 — 2017-10-30
- 2.1.0 — 2017-03-30
- 2.0.1 — 2016-11-13
- 1.1.0 — 2016-11-11
- 1.0.0 — 2016-02-03
- 0.4.0 — 2012-08-16
- 0.3.5 — 2012-08-16
- 0.3.4 — 2012-08-16
- 0.3.3 — 2012-08-16
- 0.3.2 — 2012-08-16
- 0.3.1 — 2012-08-16
- 0.3.0 — 2012-05-21
- 0.2.1 — 2012-05-20
- … 2 more at https://npm.io/package/httpdispatcher/versions

## README

httpdispatcher - basic dispatcher for node.js
=======

httpdispatcher is a simple class allows developer to have a clear dispatcher for dynamic pages and static resources.
Classes http.ServerRequest and http.ServerResponse earns new params property containing a map of received HTTP parameters.
Using httpdispatcher is pretty simple:

```js
    var HttpDispatcher = require('../httpdispatcher');
    var http           = require('http');
    var dispatcher     = new HttpDispatcher();

    dispatcher.setStatic('/resources');
    dispatcher.setStaticDirname('static');

    dispatcher.onGet("/page1", function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Page One');
    }); 

    dispatcher.onPost("/page2", function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Page Two');
    });

    dispatcher.beforeFilter(/\//, function(req, res, chain) { //any url
        console.log("Before filter");
        chain.next(req, res, chain);
    });

    dispatcher.afterFilter(/\//, function(req, res, chain) { //any url
        console.log("After filter");
        chain.next(req, res, chain);
    });

    dispatcher.onError(function(req, res) {
        res.writeHead(404);
        res.end();
    });

    http.createServer(function (req, res) {
        dispatcher.dispatch(req, res);
    }).listen(1337, '127.0.0.1');
    
    
    /*
    GET  /page1  => 'Page One'
    POST /page2  => 'Page Two'
    GET  /page3  => 404
    GET  /resources/images-that-exists.png => Image resource
    GET  /resources/images-that-does-not-exists.png => 404
    */
``` 

request and response
---------

Every listeners is called with two parameters `request` and `response`.

Request object is an instance of [`http.ClientRequest`](https://nodejs.org/api/http.html#http_class_http_clientrequest) with some custom properties:

- bodyBuffer : [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) (available only on POST request)
- body : String (available only on POST request)
- params : Object

Response object is an instance of [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse).

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