1.3.0 • Published 9 years ago

connect-dispatcher v1.3.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

connect-dispatcher

Codeship Status for unknownexception/connect-dispatcher

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.
  3. 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 and tests.

Getting started

First, install packages via npm:

npm install connect-dispatcher

Second, create a new file app.js:

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:

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:

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

h1= title

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

<h1> Hello World! </h1>

Options

Full config example:

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;

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.4.4

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.12

10 years ago

0.2.11

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago