{"id":15627320,"url":"https://github.com/tmeasday/meteor-router","last_synced_at":"2025-04-06T19:14:02.147Z","repository":{"id":3678461,"uuid":"4748125","full_name":"tmeasday/meteor-router","owner":"tmeasday","description":null,"archived":false,"fork":false,"pushed_at":"2014-04-11T13:49:57.000Z","size":933,"stargazers_count":366,"open_issues_count":39,"forks_count":72,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-03-30T18:09:16.361Z","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/tmeasday.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-22T06:07:46.000Z","updated_at":"2023-11-29T19:06:32.000Z","dependencies_parsed_at":"2022-08-19T01:00:29.012Z","dependency_job_id":null,"html_url":"https://github.com/tmeasday/meteor-router","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmeasday%2Fmeteor-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmeasday%2Fmeteor-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmeasday%2Fmeteor-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmeasday%2Fmeteor-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tmeasday","download_url":"https://codeload.github.com/tmeasday/meteor-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247535521,"owners_count":20954576,"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-03T10:16:12.535Z","updated_at":"2025-04-06T19:14:02.126Z","avatar_url":"https://github.com/tmeasday.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Meteor Router\n\nMeteor Router builds on [page.js](https://github.com/visionmedia/page.js) to provide reactive, filterable routing for [Meteor](http://www.meteor.com/) applications.\n\n## NOTE\n\nNote that the router package is **deprecated**. Work has now shifted to [Iron Router](https://github.com/EventedMind/meteor-iron-router). Please consider using IR instead of Router on new projects!\n\n## Installation\n\nMeteor Router can be installed with [Meteorite](https://github.com/oortcloud/meteorite/). From inside a Meteorite-managed app:\n\n``` sh\n$ mrt add router\n```\n\nNote that Router version 0.4.3 works with Meteor 0.5.8 and later, and 0.4.2 works with Meteor 0.5.7 and earlier.\n\n## API\n\n### Basics\n\nTo get the current page:\n\n``` javascript\nMeteor.Router.page();\n```\n\nThis is a reactive variable which will trigger invalidations as the app changes pages. Usually, you'll just want to render the template that corresponds to the current page using the following helper that finds the template by name:\n\n``` handlebars\n{{\u003e renderPage}}\n```\n\nIt's common to render the inside page isolated from the layout:\n\n``` handlebars\n{{#isolate}} {{\u003e renderPage}} {{/isolate}}\n```\n\nTo define a route, simply specify the URL it matches and the name of the template it should render. If you want to get fancy, you can specify a reactive function that returns a template name. It will get repeatedly executed as its reactive dependencies change.\n\nBe careful not to specify your routes inside the ```Meteor.startup``` function, or the routing won't work for the first load.\n``` javascript\nMeteor.Router.add({\n  '/news': 'news',\n\n  '/about': function() {\n    if (Session.get('aboutUs')) {\n      return 'aboutUs';\n    } else {\n      return 'aboutThem';\n    }\n  },\n\n  '*': 'not_found'\n});\n```\n\nTo navigate to such a URL from in the app, either create a link which links to the URL (the router will intercept clicks and trigger relevant state changes), or call directly:\n\n``` javascript\nMeteor.Router.to('/news');\n```\n\nNote that this doesn't reload the app, it instead uses HTML5 `pushState` to change the URL whilst remaining loaded.\n\n### Route functions\n\nWhen the route function is called, `this` corresponds to a page.js [`Context`](https://github.com/visionmedia/page.js#context) object, allowing you to do the following:\n\n``` javascript\nMeteor.Router.add({\n  'posts/:id': function(id) {\n    console.log('we are at ' + this.canonicalPath);\n    console.log(\"our parameters: \" + this.params);\n\n    // access parameters in order a function args too\n    Session.set('currentPostId', id);\n    return 'showPost';\n  }\n});\n```\n\n### Named Routes\n\nIf you specify your route with simply a template name, then you'll set up a _named route_. So instead of calling `Meteor.Router.to('/news')`, you can call `Meteor.Router.to('news')` (`news` is the name of the route). Additionally, that named route sets up the following:\n\n  - `Meteor.Router.newsPath()` -- which is `/news` in this case\n  - `Meteor.Router.newsUrl()` -- which is `http://yourhost.com/news`\n  - `{{newsPath}}` and `{{newsUrl}}` -- handlebars helpers\n\nIf you are using a routing function, you'll need to manually supply a route name, like so:\n\n```js\nMeteor.Router.add({\n  '/about': { as: 'about', to: function() {\n    if (Session.get('aboutUs')) {\n      return 'aboutUs';\n    } else {\n      return 'aboutThem';\n    }\n  }}\n});\n\nMeteor.Router.aboutPath(); // == '/about'\n```\n\nAdditionally, you can provide a `and` property, which is a function that executes everytime the route executes (useful if your _template_ is always the same, but you want to have some side effects):\n\n\n```js\nMeteor.Router.add({\n  '/posts/:_id': { to: 'showPost', and: function(id) {\n    Session.set('currentPostId', id);\n  }}\n});\n\nMeteor.Router.showPostPath(post) // == /posts/7\n````\n\nIf your URL has named matches inside it, you can either pass in an object with those properties (e.g. `post = {_id: 7}` above), or you can pass the arguments in in order (e.g. `showPostPath(7)`);\n\n### beforeRouting\n\nUse `Meteor.Router.beforeRouting = function() {}` to set a callback to run before any routing function. Useful to reset session variables.\n\n### Filtering\n\nThe current system of filtering in this package is the equivalent of an `after_filter` in Rails. To add a filter which will render the correct template for a page which requires login:\n\n``` javascript\nMeteor.Router.filters({\n  'checkLoggedIn': function(page) {\n    if (Meteor.loggingIn()) {\n      return 'loading';\n    } else if (Meteor.user()) {\n      return page;\n    } else {\n      return 'signin';\n    }\n  }\n});\n```\n\nTo turn the filter on, use one of:\n\n``` javascript\n// applies to all pages\nMeteor.Router.filter('checkLoggedIn');\n\n// applies to specific pages\nMeteor.Router.filter('checkLoggedIn', {only: 'profile'});\nMeteor.Router.filter('checkLoggedIn', {except: 'home'});\n\n// accepts an array of pages\nMeteor.Router.filter('checkLoggedIn', {only: ['profile', 'notifications'] });\nMeteor.Router.filter('checkLoggedIn', {except: ['home', 'browse'] });\n```\n\nNote that filters build on reactivity. So the URL will not change but the user will see different pages as the state of the `Meteor.user()` property changes.\n\n### Server-side routing\n\nThe router also allows a very simple server side routing function with a similar API:\n\n``` javascript\nMeteor.Router.add('/posts/:id.xml', function(id) {\n  return constructXMLForId(Posts.findOne(id));\n});\n```\n\nOptionally you can also restrict the route by HTTP method:\n\n``` javascript\nMeteor.Router.add('/posts/:id.xml', 'GET', function(id) {\n  return constructXMLForId(Posts.findOne(id));\n});\nMeteor.Router.add('/posts/:id.xml', 'DELETE', function(id) {\n  Posts.remove(id);\n  return [204, 'No Content'];\n});\n```\n\nThe arguments to the routing function are the parameters you've specified in your URL, and the `this` within the function is an object with three properties:\n\n* `this.params` -- the list of parameters, page.js style\n* `this.request` -- a [Connect](http://www.senchalabs.org/connect/) request\n* `this.response` -- a Connect response (use this to e.g. set headers on your response).\n\nYour routing function can return one of the following:\n\n* a string, the body of the response\n* a number, the http status code\n* an array, in one of the following forms:\n  * `[body]`\n  * `[statusCode, body]`\n  * `[statusCode, headers, body]`, where `headers` is an object mapping header names to values.\n\nAlternatively, rather than a routing function, you can just provide a fixed response:\n\n``` javascript\nMeteor.Router.add('/404', [404, \"There's nothing here!\"]);\n```\n\nThe server side router adds the bodyParser middleware, enabling automatic parsing of JSON, Mutipart and URL encoded forms. You can tweak the Multipart parsing, i.e. changing the uploaded files destination directory:\n\n``` javascript\nMeteor.Router.configure({\n  bodyParser: {\n    uploadDir: 'uploads',\n    hash: 'sha1'\n  }\n});\n```\n\nThe configure() call **MUST** be put before any add() call, otherwise it will throw an error.\nFor more information on options available, go to https://github.com/felixge/node-formidable\n\n**NOTE**: Spark (meteor's template engine) does not currently run server side, so you are limited in what you can return here. Most likely you will want to return fairly simple things like JSON or XML documents, the construction of which is up to you.\n\n## Examples\n\nCheck out `examples/simple-routed-app` for an extremely simple example of a filtered routed app. (To run, use meteorite: `cd examples/simple-routed-app; mrt run`).\n\nAdditionally, you might want to read [my blog post](http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com) on page transitions in Meteor.\n\n## Internet explorer 8+ support\n\nIf you want the router to work in older version of Internet Explorer that don't support pushState, you can use the [HTML5-History-API](https://github.com/devote/HTML5-History-API) polyfill:\n```bash\n  mrt add HTML5-History-API\n```\n\n## Older Versions of Meteor\n\n(Versions prior to v0.8.0) use `{{renderPage}}` instead of `{{\u003erenderPage}}`\n## Contributing\n\nTo run the tests, ensure that the router is checked out to a folder called `router`, and then simply run:\n\n``` sh\n$ mrt test-packages router\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmeasday%2Fmeteor-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftmeasday%2Fmeteor-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmeasday%2Fmeteor-router/lists"}