{"id":16760109,"url":"https://github.com/lgvo/express-args-resolver","last_synced_at":"2025-10-12T02:11:29.358Z","repository":{"id":36390881,"uuid":"40695771","full_name":"lgvo/express-args-resolver","owner":"lgvo","description":"a resolver of arguments for express","archived":false,"fork":false,"pushed_at":"2015-09-02T05:56:19.000Z","size":144,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T10:21:20.085Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lgvo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-14T04:29:54.000Z","updated_at":"2022-06-25T22:15:17.000Z","dependencies_parsed_at":"2022-09-06T07:54:36.335Z","dependency_job_id":null,"html_url":"https://github.com/lgvo/express-args-resolver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lgvo%2Fexpress-args-resolver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lgvo%2Fexpress-args-resolver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lgvo%2Fexpress-args-resolver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lgvo%2Fexpress-args-resolver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lgvo","download_url":"https://codeload.github.com/lgvo/express-args-resolver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243847056,"owners_count":20357317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T04:22:22.397Z","updated_at":"2025-10-12T02:11:24.311Z","avatar_url":"https://github.com/lgvo.png","language":"JavaScript","readme":"# Express Arguments Resolver\n\n[![Build Status](https://travis-ci.org/lgvo/express-args-resolver.svg?branch=master)](https://travis-ci.org/lgvo/express-args-resolver)\n[![Coverage Status](https://coveralls.io/repos/lgvo/express-args-resolver/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/lgvo/express-args-resolver?branch=master)\n[![npm version](https://badge.fury.io/js/express-args-resolver.svg)](http://badge.fury.io/js/express-args-resolver)\n[![Code Climate](https://codeclimate.com/github/lgvo/express-args-resolver/badges/gpa.svg)](https://codeclimate.com/github/lgvo/express-args-resolver)\n[![npm](https://img.shields.io/npm/dm/express-args-resolver.svg)](https://www.npmjs.com/package/express-args-resolver)\n\n\nResolve function arguments to express objects based on the arguments names.\n\n## Installation\n\n```sh\n$ npm install --save express-args-resolver\n```\n\n## Usage\n\n###  Express app\n\n```javascript\nvar express = require('express'),\n    bodyParser = require('body-parser'),\n    argsResolver = require('express-args-resolver');\n\n\nvar expressApp = express();\nexpressApp.use(bodyParser.text());\n\n// to simplify the use of argsResolver.proxy\nvar app = {\n    use: function(path, func) {\n        expressApp.use(path, argsResolver.proxy(func));\n    }\n};\n\n// the endpoints definitions go Here ...\n\nexpressApp.listen(3000, function() {\n    console.log('Up and Running!');\n});\n```\n\nThe proxy function will create a closure resolving the arguments by name.\n\n### Request.params\n\n```javascript\napp.use('/param/:id', function(id, res) {\n    res.send(id);\n});\n```\n\nIn that case the closure will work someway like this:\n```javascript\nfunction inner(id, res) {\n    res.send(id);\n}\n\nfunction out(req, res, next) {\n    var id = req.params['id'] || req.query['id'];\n    return inner(id, res);\n}\n```\n\nSo we can call this endpoint from curl to test it:\n```shell\n$ curl http://localhost:3000/param/1234\n1234\n```\n\nIt will look on the resolver table trying to find a resolver for that name.\nWe 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.\n\n### Request.query\n\n```javascript\napp.use('/query', function(name, res) {\n    res.send(name);\n});\n```\n\nIn that case we don't have 'name' on the params object, we can pass it as a query:\n```shell\n$ curl http://localhost:3000/query?name=test\ntest\n```\n\n### Request.body\n\n```javascript\napp.use('/body', function(body, res) {\n    res.send(body);\n});\n```\n\nSame here using bodyParser and resolving the body argument:\n\n```shell\n$ curl http://localhost:3000/body --data \"dataSent\" --header \"Content-Type: text/plain\"\ndataSent\n```\n\n### List of resolvers\n\n* req: Request \n* request: Request\n* res: Response \n* response: Response \n* next: next (callback) \n* params: Request.params \n* query: Request.query \n* body: Request.body \n\n### Add / change resolvers\n\nYou 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.\n\n```javascript\n// resolver for user\nargsResolver.addResolver('user', function(req, res, next) {\n    return req.user;\n});\n\n// resolver for username\nargsResolver.addResolver('username', function(req, res, next) {\n    if (req.user) {\n        return req.user.username;\n    }\n});\n```\n\n### Change the default resolver\n\nThe default resolver is used to resolve any param that is not in the table.\nIf you don't change, it will look at the params for the name and if not found at the query.\n\n\nLets say you want to look at the body insted.\nYou can do that:\n\n```javascript\n\n// by default will look at body properties\nargsResolver.changeDefault(function(name) {\n    return function(req, res, next) {\n        if (req.body \u0026\u0026 req.body[name]) {\n            return req.body[name];\n        }\n    };\n});\n\n```\n\n## License\n[MIT](LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flgvo%2Fexpress-args-resolver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flgvo%2Fexpress-args-resolver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flgvo%2Fexpress-args-resolver/lists"}