This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpackage.json
37 lines (37 loc) · 7.35 KB
/
package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"name": "reversable-router",
"description": "HTTP request router with named reversable routes",
"version": "1.0.3",
"author": {
"name": "Anton Stoychev",
"email": "[email protected]"
},
"main": "./router.js",
"dependencies": {
"xregexp": "*",
"methods": "0.0.1"
},
"keywords": [
"router",
"reversable",
"express",
"web"
],
"repository": {
"type": "git",
"url": "git://github.com/web-napopa/node-reversable-router"
},
"readme": "# reversable-router\n`node.js` module for routing HTTP requests. Can be used standalone or as part of [express.js](http://expressjs.com/) framework.\n\n**Feature overview**:\n - Support for named routes\n - URLs can be generated by providing a `name` of the route and the required `parameters`\n - Support for optional parts in the route `path` (and URL generation still works with as many optional parts as you want)\n - Support for anonymous `*` parameters inside the path\n - Supports converting the last anonymous parameter to pairs of `param`=>`value` separated by `/`\n - Improved performance on literal matches\n - Supports callbacks for router parameters. Same logic as `express` native router. \n - Supports middleware route callbacks. Same logic as `express` native router. \n - Supports array of middleware route callbacks. Same logic as `express` native router. \n - Can be used standalone or as replacement for express.js routing.\n\n## Install\n\n```\nnpm install reversable-router\n```\n\n## Features\n\n### Example\n#### As a replacement for express framework router\n```js\nvar express = require('express');\nvar app = express();\n\nvar Router = require('reversable-router');\nvar router = new Router();\nrouter.extendExpress(app);\n\napp.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){\n var url = app._router.build('admin.user.edit', {id: 2}); // /admin/user/2\n});\n\n//.. and a helper in the view files:\nurl('admin.user.edit', {id: 2})\n\n```\nAlternatively to route a group of requests to the same URL but using a different HTTP method:\n```js\napp.route('/admin/user/:id', 'admin.user.edit', function(){\n app.get(function(req, res, next) {\n // show..\n })\n app.post(function(req, res, next) {\n // insert..\n })\n});\n// or even:\napp.route('/admin/user/:id', 'admin.user.edit', {\n get: function(req, res, next) {\n // show..\n },\n post: function(req, res, next) {\n // insert..\n }\n});\n```\n\n#### As a standalone\n\n```js\nvar Router = require('reversable-router')();\nvar router = new Router();\n\nrouter.add('get', '/admin/user/:id', function() {\n var url = router.build('admin.user.edit', {id: 2}); // /admin/user/2\n}, {\n name: 'admin.user.edit'\n});\n\n//...\nrouter.dispatch(req);\n```\n\n### Benefits of named routes\nYou can easily check the current route in middleware without stating the defined route path. Thus avoding duplication and keeping route paths in a central place. \n\nThis allows the path to the route to be changed as frequently while the rest of the logic across middleware or views to remain the same.\n\n### Generating URLs\nBoth example above incude a URL generation. \n\nIf you're using express:\n```js\n// in the views:\nurl('admin.user.edit', {id: 2})\n// anywhere else:\napp._router.build('admin.user.edit', {id: 2})\n```\nIf not:\n```js\nrouter.build('admin.user.edit', {id: 2});\n```\n\n### Full support for optional parts of the URL\nYou can define routes like this:\n\n```js\napp.get('/admin/(user/(edit/:id/)(album/:albumId/):session/)test', 'admin', function(req, res, next){\n console.log(req.params);\n});\n```\n\nBrackets define the limits of the optional parts. Here you have 3 optional parts. 2 of them nested in the other. \n\nIf you don't pass all the parameters inside a optional part, the part will simply be removed from the generated URL.\n\nSo in the views:\n```js\nurl('admin', {id: 4, albumId:2, session: 'qwjdoqiwdasdj12asdiaji198a#asd'});\n// will generate: /admin/user/edit/4/album/2/qwjdoqiwdasdj12asdiaji198a/test\n```\n```js\nurl('admin', {id: 4, session: 'qwjdoqiwdasdj12asdiaji198a#asd'});\n// will generate: /admin/user/edit/4/qwjdoqiwdasdj12asdiaji198a/test\n```\n```js\nurl('admin', {albumId: 2, session: 'qwjdoqiwdasdj12asdiaji198a#asd'});\n// will generate: /admin/user/album/2/qwjdoqiwdasdj12asdiaji198a/test\n```\n```js\nurl('admin', {id: 4, albumId:2);\n// will generate: /admin/test\n// because :session parameter is missing and the optional part \n// that contains it contains also the other 2 parts\n```\n\n### Improved matching speed for literal matches\nSignificant amount of the routes in an web applications are simply hardcoded strings. Things like `/admin` or `/user/login`.\nSuch routes will be matched with direct check for equallity without the need for a regular expression execution.\n\n### Anonymous `*` parameters inside the path\n```js\napp.get('/admin/*/user/*/:id/', 'admin.user.edit', function(req, res, next){\n console.log(req.params)\n});\n```\nRequesting: `/admin/any/user/thing/2` will output:\n```\n{\n _masked: [ 'any', 'thing'],\n id: '2'\n}\n```\n\nAnalogous in order to generate the same url:\n```js\nurl('admin.user.edit', {id:2, _masked: ['any','thing']})\n```\n\n### Converting the trailing `*` anonymous parameter to multiple `name:value` parameters\n```js\napp.get('/admin/*/user/*/:id/albums/*', 'admin.user.edit', function(req, res, next){\n console.log(req.params)\n}, {\n wildcardInPairs: true\n});\n```\nRequesting: `/admin/any/user/thing/2/albums/sort/name/order/desc` will output:\n```\n{\n _masked: [ 'any', 'thing'],\n id: '2',\n sort: 'name',\n order: 'desc'\n}\n```\n\nAnalogous in order to generate the same url:\n```js\nurl('admin.user.edit', {id:2, _masked: ['any','thing'], sort: 'name', 'order': 'desc'})\n```\n\n\n## Future development planned \n\n### Publish\n - Organise and publish tests\n\n### Implement\n - Query based routing and generation\n\n### Investigate\n**meta-routing** Middleware depending on media? mobile, desktop, agent\n\n## License\nThis is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or\ndistribute this software, either in source code form or as a compiled\nbinary, for any purpose, commercial or non-commercial, and by any\nmeans.\n\nIn jurisdictions that recognize copyright laws, the author or authors\nof this software dedicate any and all copyright interest in the\nsoftware to the public domain. We make this dedication for the benefit\nof the public at large and to the detriment of our heirs and\nsuccessors. We intend this dedication to be an overt act of\nrelinquishment in perpetuity of all present and future rights to this\nsoftware under copyright law.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\nOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\nARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\nFor more information, please refer to <http://unlicense.org>",
"bugs": {
"url": "https://github.com/web-napopa/node-reversable-router/issues"
},
"scripts": {
"test": "mocha test"
},
"devDependencies": {
"mocha": "~1.14.0",
"expect.js": "~0.2.0",
"sinon": "~1.7.3",
"express": "~3.4.4"
}
}