{"id":16190218,"url":"https://github.com/apsavin/router-base","last_synced_at":"2025-03-19T03:30:55.781Z","repository":{"id":16789422,"uuid":"19547897","full_name":"apsavin/router-base","owner":"apsavin","description":"Base router for your JS framework or frameworkless app","archived":false,"fork":false,"pushed_at":"2016-07-06T13:13:18.000Z","size":37,"stargazers_count":48,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-15T16:11:19.013Z","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/apsavin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-07T19:43:25.000Z","updated_at":"2022-12-18T18:26:18.000Z","dependencies_parsed_at":"2022-08-20T14:10:23.433Z","dependency_job_id":null,"html_url":"https://github.com/apsavin/router-base","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsavin%2Frouter-base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsavin%2Frouter-base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsavin%2Frouter-base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apsavin%2Frouter-base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apsavin","download_url":"https://codeload.github.com/apsavin/router-base/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243754092,"owners_count":20342542,"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-10T07:38:57.285Z","updated_at":"2025-03-19T03:30:55.452Z","avatar_url":"https://github.com/apsavin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Base router for your JS framework or frameworkless app\n\n[![Build Status](https://travis-ci.org/apsavin/router-base.svg?branch=master)](https://travis-ci.org/apsavin/router-base) [![Coverage Status](https://coveralls.io/repos/apsavin/router-base/badge.svg)](https://coveralls.io/r/apsavin/router-base) [![NPM version](https://badge.fury.io/js/router-base.svg)](http://badge.fury.io/js/router-base) [![Bower version](https://badge.fury.io/bo/router-base.svg)](http://badge.fury.io/bo/router-base)\n\nIt's abstract and knows nothing about http. It's just matches and generates urls.\n\n##How to include it in my app or framework?\n\nRouter supports:\n\n1. [node.js](http://nodejs.org) modules,\n2. [requirejs](http://requirejs.org) modules\n3. and, of course, awesome [ym](https://github.com/ymaps/modules) modules.\n4. You can just use the `\u003cscript\u003e` tag also, RouterBase will export the global variable then.\n\nYou can install it with npm:\n\n```bash\n$ npm install router-base\n```\n\nor bower:\n\n```bash\n$ bower install router-base\n```\n\n##How to use it?\n\n###Very basic example\n\nAt first, you need to create an instance:\n\n```javascript\nvar myRouter = new RouterBase({\n\n    routes: myRoutes // routes config\n\n});\n```\n\nWhere routes config is an Array with objects, each object is a configuration for a route.\nFor example:\n\n```javascript\nvar myRoutes = [{\n    id: 'simplest_route',\n    path: '/a/route'\n}];\n\nvar myRouter = new RouterBase({\n    routes: myRoutes\n});\n\n// You can generate routes by id:\nmyRouter.generate('simplest_route'); \n// returns '/a/route'\n\n// You can find a route by path and method:\nmyRouter.match({path: '/a/route', method: 'GET'}); \n// returns {id: 'simplest_route', parameters: {}, definition: { id: 'simplest_route',\n                                                                   path: '/a/route',\n                                                                   defaults: {},\n                                                                   requirements: {},\n                                                                   host: undefined,\n                                                                   methods: [ 'GET', 'POST', 'PUT', 'DELETE' ],\n                                                                   schemes: [ 'http', 'https' ] } }\n}\n\n// You can get full info about a route by id:\nmyRouter.getRouteInfo('simplest_route');\n// for answer see [tests](https://github.com/apsavin/router-base/blob/master/test/data/getRouteInfo-data.js#L4-L41)\n```\n\nYou can use access to the `definition` if you want to set additional fields to the route.\nFor example, you can mark route as secure specifying `secure: true` in the route config\nand then check this field like this:\n\n```javascript\nvar route = myRouter.match({path: '/a/route', method: 'GET'}); \n\nif (route \u0026\u0026 route.definition.secure) // do something\n\n```\n\nI will omit the `definition` field in the next examples.\n\n###Beautiful urls examples\n\n####You can use named parameters in paths of the routes.\n```javascript\nvar myRoutes = [{\n    id: 'route_with_parameter_in_path',\n    path: '/some/path/{parameter}'\n}];\n\nmyRouter.generate('route_with_parameter_in_path'); \n// will throw an Error, because parameter is needed for the route\nmyRouter.generate('route_with_parameter_in_path', {parameter: 1}); \n// returns '/some/path/1'\nmyRouter.generate('route_with_parameter_in_path', {parameter: 'value'}); \n// returns '/some/path/value'\n\nmyRouter.match({path: '/some/path/', method: 'GET'}); \n// returns null\nmyRouter.match({path: '/some/path/to', method: 'GET'}); \n// returns {id: 'route_with_parameter_in_path', parameters: {parameter: 'to'}, definition: {...}}\n```\n####Optional parameters in paths\n```javascript\nvar myRoutes = [{\n    id: 'route_with_parameter_in_path',\n    path: '/some/path/{parameter}',\n    defaults: {parameter: 1}\n}];\n\nmyRouter.generate('route_with_parameter_in_path'); \n// returns '/some/path'\nmyRouter.generate('route_with_parameter_in_path', {parameter: 1}); \n// returns '/some/path/1'\nmyRouter.generate('route_with_parameter_in_path', {parameter: 'value'}); \n// returns '/some/path/value'\n\nmyRouter.match({path: '/some/path', method: 'GET'}); \n// returns  {id: 'route_with_parameter_in_path', parameters: {parameter: 1}, definition: {...}}\nmyRouter.match({path: '/some/path/to', method: 'GET'}); \n// returns {id: 'route_with_parameter_in_path', parameters: {parameter: 'to'}, definition: {...}}\n```\n####Restricted parameters in paths\n```javascript\nvar myRoutes = [{\n    id: 'route_with_parameter_in_path',\n    path: '/some/path/{parameter}',\n    defaults: {parameter: 1},\n    requirements: {parameter: '\\\\d+'}\n}];\n\nmyRouter.generate('route_with_parameter_in_path'); \n// returns '/some/path'\nmyRouter.generate('route_with_parameter_in_path', {parameter: 1}); \n// returns '/some/path/1'\nmyRouter.generate('route_with_parameter_in_path', {parameter: 'value'}); \n// throws an Error, because parameter is not numeric\n\nmyRouter.match({path: '/some/path', method: 'GET'}); \n// returns {id: 'route_with_parameter_in_path', parameters: {parameter: 1}, definition: {...}}\nmyRouter.match({path: '/some/path/to', method: 'GET'}); \n// returns null\n```\n\n##All possible routes parameters\n\n1. id - String, required. You can use it to link a route to your controller.\n2. path - String, required. Can include named parameters in `{parameterName}` form.\n3. host - String, optional. Can include named parameters in `{parameterName}` form (For example, '{sub}.example.com').\n4. defaults - Object, optional. Keys are parameters names, values are parameters default values.\n5. requirements - Object, optional. You can use it to restrict parameters. Keys are parameters names, values are strings. Strings from values are for regular expressions, router uses it to test parameters.\n6. methods - Array, optional. For example, `['GET']` to allow only `GET` methods.\n7. schemes - Array, optional. For example, `['https']` to force https.\n\n##RouterBase parameters\n\n1. routes - an Array of routes configs, the only required parameter\n2. defaultMethods - what methods available for routes, `['GET', 'POST', 'PUT', 'DELETE']` by default\n3. defaultSchemes - what schemes available for routes, `['http', 'https']` by default\n4. symbols.parametersDelimiters. By default, `.` and `/` can be used as parameters delimiters in paths\n5. symbols.parameterStart, default value is '\\{'\n6. symbols.parameterMatcher, default value is '.*?'\n7. symbols.parameterEnd, default value is '\\}'\n\n##Where is tests?\nOf course, in `test` folder. Use `npm test` to run.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapsavin%2Frouter-base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapsavin%2Frouter-base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapsavin%2Frouter-base/lists"}