Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lgvo/express-args-resolver
a resolver of arguments for express
https://github.com/lgvo/express-args-resolver
Last synced: about 1 month ago
JSON representation
a resolver of arguments for express
- Host: GitHub
- URL: https://github.com/lgvo/express-args-resolver
- Owner: lgvo
- License: mit
- Created: 2015-08-14T04:29:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-02T05:56:19.000Z (over 9 years ago)
- Last Synced: 2024-11-15T22:30:51.217Z (about 2 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Express Arguments Resolver
[![Build Status](https://travis-ci.org/lgvo/express-args-resolver.svg?branch=master)](https://travis-ci.org/lgvo/express-args-resolver)
[![Coverage Status](https://coveralls.io/repos/lgvo/express-args-resolver/badge.svg?branch=master&service=github)](https://coveralls.io/github/lgvo/express-args-resolver?branch=master)
[![npm version](https://badge.fury.io/js/express-args-resolver.svg)](http://badge.fury.io/js/express-args-resolver)
[![Code Climate](https://codeclimate.com/github/lgvo/express-args-resolver/badges/gpa.svg)](https://codeclimate.com/github/lgvo/express-args-resolver)
[![npm](https://img.shields.io/npm/dm/express-args-resolver.svg)](https://www.npmjs.com/package/express-args-resolver)Resolve function arguments to express objects based on the arguments names.
## Installation
```sh
$ npm install --save express-args-resolver
```## Usage
### Express app
```javascript
var express = require('express'),
bodyParser = require('body-parser'),
argsResolver = require('express-args-resolver');var expressApp = express();
expressApp.use(bodyParser.text());// to simplify the use of argsResolver.proxy
var app = {
use: function(path, func) {
expressApp.use(path, argsResolver.proxy(func));
}
};// the endpoints definitions go Here ...
expressApp.listen(3000, function() {
console.log('Up and Running!');
});
```The proxy function will create a closure resolving the arguments by name.
### Request.params
```javascript
app.use('/param/:id', function(id, res) {
res.send(id);
});
```In that case the closure will work someway like this:
```javascript
function inner(id, res) {
res.send(id);
}function out(req, res, next) {
var id = req.params['id'] || req.query['id'];
return inner(id, res);
}
```So we can call this endpoint from curl to test it:
```shell
$ curl http://localhost:3000/param/1234
1234
```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
```javascript
app.use('/query', function(name, res) {
res.send(name);
});
```In that case we don't have 'name' on the params object, we can pass it as a query:
```shell
$ curl http://localhost:3000/query?name=test
test
```### Request.body
```javascript
app.use('/body', function(body, res) {
res.send(body);
});
```Same here using bodyParser and resolving the body argument:
```shell
$ 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.
```javascript
// resolver for user
argsResolver.addResolver('user', function(req, res, next) {
return req.user;
});// resolver for username
argsResolver.addResolver('username', function(req, res, next) {
if (req.user) {
return req.user.username;
}
});
```### 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:```javascript
// by default will look at body properties
argsResolver.changeDefault(function(name) {
return function(req, res, next) {
if (req.body && req.body[name]) {
return req.body[name];
}
};
});```
## License
[MIT](LICENSE)