connect-action

A simple action system for Connect

connect-action

A simple named action system for Connect

$ npm install connect-action
var connect = require('connect');
var connectAction = require('connect-action');
 
var pages = {
  "/": { action: 'root' },
  "/login": { action: 'login', body: 'Login page' }
};
 
var actions = connectAction(function (action) {
  for (var url in pages) {
    if(pages[url].action === action.name) return url;
  }
});
 
actions.get('root', function (reqres) {
  var url = req.resolve.action('login');
  res.end('<a href="' + url + '">Log in</a>');
});
 
actions.get('login', function (reqres) {
  res.end(req.page.body);
});
 
var app = connect().use(actions);
app.use(function (reqresnext) {
  req.page = pages[req.url];
  if(!req.page) return next();
  actions.run(req.page.action, req, res, next);
});
app.listen(3000);

The connect-action module adds a couple of helper functions onto the request and response objects

// attempt to find a url for a given action:
req.resolve.action('action-name');

// if a redirect function exists, you can also redirect to actions:
res.redirect.action('action-name');