# connect-dispatcher

> convetions-over-configuration dispatcher middleware in ES6

Latest version **1.3.0** (published 2015-07-09) · MIT license · 0 weekly downloads

## Install

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

## 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.3.0 |
| Published | 2015-07-09 |
| First published | 2014-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | unknownexception |
| Maintainers | unknownexception |
| Keywords | connect, dispatcher, middleware |

## Links

- npm: https://www.npmjs.com/package/connect-dispatcher
- Repository: https://github.com/unknownexception/connect-dispatcher
- Issues: https://github.com/unknownexception/connect-dispatcher/issues
- npm.io page: https://npm.io/package/connect-dispatcher

## Dependencies (1)

- [jade](https://npm.io/package/jade.md) ^1.3.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2015-07-09
- 1.2.0 — 2015-05-19
- 1.1.0 — 2015-02-26
- 1.0.5 — 2015-02-07
- 1.0.4 — 2015-02-07
- 1.0.3 — 2015-01-27
- 1.0.2 — 2015-01-26
- 1.0.1 — 2015-01-25
- 1.0.0 — 2015-01-25
- 0.4.4 — 2014-08-25
- 0.4.2 — 2014-08-13
- 0.4.1 — 2014-06-01
- 0.4.0 — 2014-05-31
- 0.3.0 — 2014-04-20
- 0.2.12 — 2014-04-08
- … 21 more at https://npm.io/package/connect-dispatcher/versions

## README

connect-dispatcher
==================
[ ![Codeship Status for unknownexception/connect-dispatcher](https://www.codeship.io/projects/11527a50-f90d-0131-a739-5e0e92dbe03c/status)](https://www.codeship.io/projects/28799)

_Disclaimer_. `connect-dispatcher` is outdated. See koajs, rest, react-router or other frameworks. 

Why?

1. Conventions over configurations. DRY routes.
2. It has to be simple. A controller returns data to a views, rendering the different type of views depend on the context.
You can process the first response on the server-side, then render the same view in the browser.
2. Caching controllers, views, or the entire html. Cache stored in a memory, but there is no a big deal about moving to Redis.
4. Small size footprint, just single file src/dispatcher.js written with typed Flow ES6.

For more information see [examples](https://github.com/unknownexception/connect-dispatcher/tree/master/examples) and [tests](https://github.com/unknownexception/connect-dispatcher/tree/master/test).

## Getting started

First, install packages via npm:

```JavaScript
npm install connect-dispatcher
```

Second, create a new file `app.js`:

```JavaScript
var app = require('connect')(),
  dispatcher = require('connect-dispatcher');

app.use(dispatcher());

app.listen(3001);
```

By default the connect-dispatcher trying to look for controller with the name `app/controllers/pages_controller.js` whithing exported method `index`. So to avoid 404 error we need as simple controller as this `pages_controller.js`:

```JavaScript
var pages = module.exports;

pages.index = function () {
  return this.asText('Hello World!');
};
```

It produces simple plain-text response to the browser, without view rendering. To enable template-engine rendering, change code to:

```JavaScript
var pages = module.exports;

pages.index = function () {
  return {
    title:'Hello World!'
  };
};
```

and add to project a jade template. By default, the location of this view is `app/views/pages/index.jade`

```Jade
h1= title
```

After server is restarted, refresh the browser to see html response (if you are not using livereload):

```HTML
<h1> Hello World! </h1>
```


## Options

Full config example:

```JavaScript
app.use(dispatcher({
  routes: { '/' : '/pages/home'},
  controllersPath:  'application/controllers',
  getControllerFile: function (name) { return name + '_controller.js';},
  viewsPath: 'application/views', // by default it depends on NODE_ENV (useful for grunt usemin)
  getViewFile: function (controller, action) { return controller + '/' + action + '.jade';}
  cache : true, // by default it depends on NODE_ENV
  renderHook: function (ctx) { console.log ('before render ' + ctx.request.controller + '/' + ctx.request.action)}

}))
```

## TODO

-[] DI;
-[] Remove all dependencies;

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