Express Arguments Resolver
Resolve function arguments to express objects based on the arguments names.
Installation
$ npm install --save express-args-resolver
Usage
Express app
var express = bodyParser = argsResolver = ; var expressApp = ;expressApp; // to simplify the use of argsResolver.proxyvar app = { expressApp; }; // the endpoints definitions go Here ... expressApp;
The proxy function will create a closure resolving the arguments by name.
Request.params
app;
In that case the closure will work someway like this:
{ res;} { var id = reqparams'id' || reqquery'id'; return ;}
So we can call this endpoint from curl to test it:
$ curl http://localhost:3000/param/12341234
It will look on the resolver table trying to find a resolver for that name. We have one for res that pass the Response object, if it can't find one a default will be created, will look for the argument name on the params and on the query in that order.
Request.query
app;
In that case we don't have 'name' on the params object, we can pass it as a query:
$ curl http://localhost:3000/query?name=testtest
Request.body
app;
Same here using bodyParser and resolving the body argument:
$ curl http://localhost:3000/body --data "dataSent" --header "Content-Type: text/plain"dataSent
List of resolvers
- req: Request
- request: Request
- res: Response
- response: Response
- next: next (callback)
- params: Request.params
- query: Request.query
- body: Request.body
Add / change resolvers
You can add your own resolver. If you are using something like passport for auth, maybe you want to have a resolver for user and/or username.
// resolver for userargsResolver; // resolver for usernameargsResolver;
Change the default resolver
The default resolver is used to resolve any param that is not in the table. If you don't change, it will look at the params for the name and if not found at the query.
Lets say you want to look at the body insted. You can do that:
// by default will look at body propertiesargsResolver;