route.js provides a lightweight isomorphic routing solution suitable for server-side and client-side applications. With its dependency injection feature and simple API, developers can easily define and manage routes, ensuring optimal flexibility and control while building web applications.
route.js is a lightweight and straightforward isomorphic routing library designed for use on both the server and client-side applications. It incorporates dependency injection to provide arguments directly into the callback functions, enhancing the flexibility of routing logic.
This library consists solely of a router component, allowing developers to seamlessly integrate it into their own applications. Users are required to implement their code to handle route changes, which can be triggered by actions such as hash changes or through the HTML5 History API.
Key Features
- Simplicity: A minimalistic approach to routing, keeping the functionality focused and easy to use.
- Flexibility: Supports arrow functions from version 0.4.0, making the routing definitions more concise.
- Dependency Injection: Functions can receive parameters directly from the routes, simplifying callback management.
Example Usage
var router = new route();
router.match('/foo/bar/{{id}}/{{name}}', function(name, id) {
console.log(name + ' ' + id);
});
router.exec(location.hash.replace(/^#/, ''));
window.addEventListener('hashchange', function() {
router.exec(location.hash.replace(/^#/, ''));
});
API Methods
- router::route_parser(openTag, closeTag): Parses routes and produces an object containing the compiled regex and parameter names.
- router::extract_names(fn): Retrieves parameter names from the provided function.
- router::test(route, path): Validates if a given path matches a specified route pattern.
- router::match(path, fn[, data]): Establishes a new route and associates a handler function and optional data.
- router::exec(url[, init]): Executes the matching route for a given URL, invoking the corresponding function.
- router::map(pattern, url): Maps variables from a URL based on the provided pattern, returning an object with extracted variable values.
- router::pick(array, url): Filters routes from an array that match the provided URL and returns the corresponding data.
route.js provides developers with a robust foundation for managing routes in their applications, promoting clean and organized code structure.
No comments yet.
Sign in to be the first to comment.