Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

controller

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

controller - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

2

package.json
{
"name": "controller",
"version": "0.3.0",
"version": "0.4.0",
"description": "an action controller for express",

@@ -5,0 +5,0 @@ "main": "lib/controller.js",

@@ -41,2 +41,3 @@ # controller

* [Usage](#constructor)
* [Middleware Groups](#groups)
* [define](#define) - define handlers

@@ -66,4 +67,4 @@ * [middleware](#middleware) - add middleware for handlers

// It also works to attach it as a route, which will prefix all of the routes in
// the controller with that path.
// It also works to attach it with a route, which will prefix all of the routes
// in the controller with that path.

@@ -75,2 +76,46 @@ app.use('/users/', users);

<a name="groups"/>
### Middleware Groups
Controller introduces the idea of middleware groups. This allows to you specify
named groups, and apply to middleware to every handler that is labelled with
this group. For example, you might have bunch of handlers that require you to
be logged in, and some middleware which checks authentication. You could add
all of the handlers to the 'require-login' group, and then add your auth
middleware to the 'reguire-login' group. This will now apply that middleware to
every handler in the group.
In action:
```javascript
var stuff = controller();
// define handlers with their groups
stuff.define('sensitive-thing', ['require-login'], function(req,res){});
stuff.define('secret-thing', ['require-login'], function(req,res){});
// define middleware for the group
stuff.middleware('require-login', function(req, res, next) {
if (isAuthenticated(req)) {
next();
} else {
res.send(403);
}
})
```
__Special Groups__
There are some special groups by default. The first one is `all`, which applies
middleware to every action on this controller. Apart from that, every action
name is also a middleware group, so you can add middleware to individual actions
__Middleware Ordering__
1. `'all'` grouped middleware is executed first.
2. all other groups are then executed in the order they were added to the route
with `route` or `direct`. within the group, middlewares are executed in the
order they were added.
3. route specific middleware is then executed in the order it was added.
<a name="define"/>

@@ -105,3 +150,3 @@ ### define(name, [groups], handler)

<a name="middleware"/>
### middleware(group*, middleware*)
### middleware(group\*, middleware\*)

@@ -120,10 +165,2 @@ Define some middleware(s) for a group(s). More than one middleware can be

__Middleware Execution Order__
1. `'all'` grouped middleware is executed first.
2. all other groups are then executed in the order they were added to the route
with `route` or `direct`. within the group, middlewares are executed in the
order they were added.
3. route specific middleware is then executed in the order it was added.
__Example__

@@ -172,3 +209,3 @@

<a name="direct"/>
### direct(method, path, [middleware/groups...,] handlerfn)
### direct(method, path, [middleware\*/groups\*,] handlerfn)

@@ -175,0 +212,0 @@ Directly route a function optionally with some middleware. This is essentially

@@ -68,2 +68,19 @@ var assert = require('assert');

})
it('should allow app nesting', function(done) {
var c = ctrl();
var c0 = ctrl();
c.direct('get', '/action', routestr('test'));
var app = express();
c0.app.use('/second/', c);
app.use('/first/', c0);
req(app)
.get('/first/second/action')
.expect(200)
.expect('test')
.end(done);
});
it('should route with a prefix from middleware', function(done) {

@@ -70,0 +87,0 @@ var c = ctrl();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc