connect-traversal
connect-traversal is a middleware for Connect and Express framework that allows to use URL traversal instead of URL dispatching.
Traversal mechanism is more powerful then Url dispatching and used in popular frameworks such as Rails, Pyramid.
For registering new resource just call method where you should specify unique resource name and object with your methods and properties.
traversal.registerResource('resourceName', {...});
Resource prototype has methods and properties that can be overridden.
Properties:
key: - Resource key
parent: parent Resource
options: custom properties can be set here
resource: resourceName - unique id of resource.
children: {'key': 'resourceName'} - map of (key, resource id) determining children resources through fixed key.
child: 'resourceName' - child resource that can be created through a custom id-like key.
Methods:
init() - method that triggering in constructor. Here can be specified special actions.
childValidate(key) - method allows to create special rules for key validation.
After registration, you can retrieve instance.
var resource = traversal.initResource('resourceName', 'key', parentResource, options);
And retrieve for example child resource by key that was specified in children property.
var childResource = resource.get('key'); // childResource.parent == resource
Example:
var traversal = require('connect-traversal');
var newsResource = {
getNews: function() {
// Logic depends on parent resource.
if (this.parent.resource == 'userResource') {
// parent resource is UserResource
var userId = this.parent.key
// get news for user...
} else {
// parent resource is RootResource
// get common news for all users...
}
}
}
traversal.registerResource('newsResource', newsResource);
var userResource = {
children: {'news': 'newsResource'},
getUser: function() {
...
}
}
traversal.registerResource('userResource', userResource);
var usersResource = {
child: 'userResource',
childValidate: function(key) {
return !!key.match(/\d+/);
}
}
traversal.registerResource('usersResource', usersResource);
var rootResource = {
children: {
'users': 'usersResource',
'news': 'newsResource'
}
};
traversal.registerResource('rootResource', rootResource);
...
var root = traversal.initResource('rootResource');
var user = root.get('users').get('123'); // userResource with key '123'
var news1 = root.get('news') // newsResource with parent rootResource
var news2 = user.get('news') // newsResource with parent userResource with key '123'
For auto-creation resource chain on the url in Connect application, this app should use traversal middleware.
var traversal = require('connect-traversal');
app.use(traversal.middleware);
Also paths with callbacks should be registered:
traverse.registerPath('resourceName', options, callback1, callback2, ...)
options object where can be specified HTTP method, parent resource and name appendix for filtering and special behavior.
{method, parent, name}
// this callbacks will trigger on GET /users/user/123, but for POST 404 will be thrown.
traverse.registerPath('userResource', {method: 'get'}, function(req, res) {
var userResource = req.resource //UserResource for user 123
var parent = resource.parent //UsersResource
parent.createUser({id: '222'});
var anotherUserResource = parent.get('222'); //UserResource for user 222
});
// will trigger for POST /users/users/create with special appendix path name only
traverse.registerPath('usersResource', {method: 'post', name: 'create'}, function(req, res) {
// req.pathname = 'create'
var resource = req.resource //UsersResource
});
// will trigger for GET /users/users/random/222/333 with special appendix path name only
traverse.registerPath('users', {method: 'post', name: 'random'}, function(req, res) {
// req.pathname = 'random'
// also contains subpath list
// req.subpath = ['222', '333']
var resource = req.resource //UsersResource
res.send('users');
});
// will trigger on GET, POST, any HTTP method /users/123/news
// but not for /news because of specified parent resource.
traverse.registerPath('news', {parent: 'userResource'}, function(req, res) {
req.parent // RootResource
var resource = req.resource //NewsResource
res.send(resource.getNews());
});