A comprehensive router package for Meteor, built specifically for the koad:io framework. This package combines multiple Iron Router repositories into a single, streamlined package.
koad:io-router unifies the following repositories into a single cohesive package:
- iron-router - Core routing functionality
- iron-controller - Controller layer
- iron-layout - Layout management
- iron-dynamic-template - Dynamic template handling
- iron-middleware-stack - Middleware implementation
- iron-core - Core functionality
- iron-location - Location handling
- iron-url - URL parsing and manipulation
meteor add koad:io-routerCreate routes in a client/server JavaScript file:
Router.route('/', function () {
this.render('MyTemplate');
});
Router.route('/items', function () {
this.render('Items');
});
Router.route('/items/:_id', function () {
var item = Items.findOne({_id: this.params._id});
this.render('ShowItem', {data: item});
});
// Server-side route
Router.route('/files/:filename', function () {
this.response.end('hi from the server\n');
}, {where: 'server'});
// RESTful API routes
Router.route('/api', {where: 'server'})
.get(function () {
this.response.end('get request\n');
})
.post(function () {
this.response.end('post request\n');
});- Client and Server Routing: Works seamlessly on both client and server
- RESTful Routes: Support for RESTful API endpoints
- Template Integration: Automatic template rendering based on route configuration
- Middleware Support: Hook into the routing lifecycle with middleware
- Layout Management: Control layouts and nested templates
- Dynamic Templates: Render templates dynamically based on route data
- Parameter Extraction: Easy access to URL parameters and query strings
The router provides several hooks for controlling the routing flow:
Router.onBeforeAction(function() {
if (!Meteor.userId()) {
this.render('login');
} else {
this.next();
}
});If you don't explicitly set a template option on your route and don't explicitly render a template name, the router will try to automatically render a template based on the name of the route:
Router.route('/items/:_id', {name: 'items.show'});
// Will look for a template named 'ItemsShow'To customize this behavior, set your own converter function:
Router.setTemplateNameConverter(function (str) { return str; });You can use the waitOn option to make sure data is available before rendering:
Router.route('/post/:_id', {
name: 'post.show',
waitOn: function() {
return Meteor.subscribe('post', this.params._id);
}
});Access query parameters through the query object:
Router.route('/search', function() {
var keyword = this.params.query.keyword;
this.render('searchResults', {data: {keyword: keyword}});
});Contributions to koad:io-router are welcome! Whether it's bug fixes, feature enhancements, or documentation improvements, your help is appreciated.
When reporting issues, please include:
- A clear description of the problem
- Steps to reproduce the issue
- Expected vs. actual behavior
- A minimal reproduction case if possible
- Clone the repository
- Set up a local packages directory
- Add the package to your Meteor project for testing
export PACKAGE_DIRS="/path/to/your/packages"
git clone https://github.com/koad/io-router.git /path/to/your/packages/koad-io-router
cd your-meteor-project
meteor add koad:io-routerMIT