URL routing inspired by express for the frontend!
Aviation automatically tracks link clicks, history events and page loads, and invokes your functions.
- Create dynamic websites (especially dashboards) easily.
- In-built response functions for stuff like changing content and setting cookies.
- Parses URLs, queries, cookies and parameters for you.
First include Aviation in your code
Let's create a simple app that shows a welcome message on page load!
var app =app
Now, let's unleash Aviation's capabilities with a dynamic example.
In this example, we'll create a menu on page load.
We'll be able to browse to different pages, without actually reloading the page.
var app =appapp
Here's the result:
This creates a new instance of Aviation with the supplied options. Possible options are:
- contentWrapper (String) This is the element res.html() will update with content. Defaults to
- onError (Function) This is the function that will be called when an error occurs. Defaults to
- caseSensitive - (Boolean) Should /app and /App not be considered the same? Defaults to false.
- strict - (Boolean) Should /app and /app/ not be considered the same? Defaults to false.
- removeFromPath - (String or Regex) Removes the string or matching regex from all paths. Useful for running local projects, or anything inside a directory.
- event - (String) The event type to listen for. Defaults to
- source - (String) The source types to listen for. Defaults to
.use(callback) or .use(path, callback)
This function registers a callback that Aviation will call when a request that matches its path is received. It also has an alias,
The path can be an array of paths, or a simple string, or a regex. When a path isn't supplied, all requests are routed to that callback.
You can specify parameters in the path. Aviation will expose these using
You can also specify wildcards. For example, Aviation will send a request for /users/dev/orders to the following function.
This function handles loading a new URL. It can also handle events. Advanced usages can directly call this when they want to use Aviation rather than waiting for someone to click a link.
Whenever Aviation invokes a callback, it provides three functions as arguments.
- url - This is the URL of the request.
- hostname - This is the hostname (domain) of the request.
- pathname - This is the path of the request, like /app.
- protocol - This is the protocol of the request.
- secure - Boolean, whether the request is over HTTPS.
- query - Key-value object, this is the parsed query string of the URL.
- params - Key-value object, these are the parameters for URL requests.
- cookies - Key-value object, these are the parsed browser cookies.
- aviation - A reference to the Aviation instance.
- cookie(key, value, options) - Saves a cookie in the browser. Options can have:
- clearCookie(key, options) - Deletes a cookie from the browser. Options other than
expiresmust be the same or cookie deletion won't work.
- redirect(url) - Redirects to this URL. If Aviation has a callback for this URL, it'll be called, else a real redirection will occur. It also has an alias,
- page(title, url) - Updates the page title and creates a history event. If no URL is specified, the request URL will be used.
- html(content) - Essentially a wrapper for jQuery's .html(). Updates the content of the page.
next() makes Aviation run the next matching function for this request.