# page.js **Repository Path**: mirrors_codejamninja/page.js ## Basic Information - **Project Name**: page.js - **Description**: Micro client-side router inspired by the Express router - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2025-10-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README  Tiny Express-inspired client-side router. [](https://travis-ci.org/visionmedia/page.js) [](https://coveralls.io/r/visionmedia/page.js?branch=master) [](https://gitter.im/visionmedia/page.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) ```js page('/', index) page('/user/:user', show) page('/user/:user/edit', edit) page('/user/:user/album', album) page('/user/:user/album/sort', sort) page('*', notfound) page() ``` ## Installation There are multiple ways to install `page.js`. With package managers: ```bash $ npm install page # for browserify $ component install visionmedia/page.js $ bower install visionmedia/page.js ``` ## Running examples To run examples do the following to install dev dependencies and run the example server: $ git clone git://github.com/visionmedia/page.js $ cd page.js $ npm install $ node examples $ open http://localhost:4000 Currently we have examples for: - `basic` minimal application showing basic routing - `notfound` similar to `basic` with single-page 404 support - `album` showing pagination and external links - `profile` simple user profiles - `query-string` shows how you can integrate plugins using the router - `state` illustrates how the history state may be used to cache data - `server` illustrates how to use the dispatch option to server initial content - `chrome` Google Chrome style administration interface - `transitions` Shows off a simple technique for adding transitions between "pages" - `partials` using hogan.js to render mustache partials client side __NOTE__: keep in mind these examples do not use jQuery or similar, so portions of the examples may be relatively verbose, though they're not directly related to page.js in any way. ## API ### page(path, callback[, callback ...]) Defines a route mapping `path` to the given `callback(s)`. Each callback is invoked with two arguments, [context](#context) and `next`. Much like Express invoking next will call the next registered callback with the given path. ```js page('/', user.list) page('/user/:id', user.load, user.show) page('/user/:id/edit', user.load, user.edit) page('*', notfound) ``` Under certain conditions, links will be disregarded and will not be dispatched, such as: - Links that are not of the same origin - Links with the `download` attribute - Links with the `target` attribute - Links with the `rel="external"` attribute ### page(callback) This is equivalent to `page('*', callback)` for generic "middleware". ### page(path) Navigate to the given `path`. ```js $('.view').click(function(e){ page('/user/12') e.preventDefault() }) ``` ### page(fromPath, toPath) Setup redirect from one path to another. ### page.redirect(fromPath, toPath) Identical to `page(fromPath, toPath)` ### page.redirect(path) Calling page.redirect with only a string as the first parameter redirects to another route. Waits for the current route to push state and after replaces it with the new one leaving the browser history clean. ```js page('/default', function(){ // some logic to decide which route to redirect to if(admin) { page.redirect('/admin'); } else { page.redirect('/guest'); } }); page('/default'); ``` ### page.show(path) Identical to `page(path)` above. ### page([options]) Register page's `popstate` / `click` bindings. If you're doing selective binding you'll like want to pass `{ click: false }` to specify this yourself. The following options are available: - `click` bind to click events [__true__] - `popstate` bind to popstate [__true__] - `dispatch` perform initial dispatch [__true__] - `hashbang` add `#!` before urls [__false__] - `decodeURLComponents` remove URL encoding from path components (query string, pathname, hash) [__true__] If you wish to load serve initial content from the server you likely will want to set `dispatch` to __false__. ### page.start([options]) Identical to `page([options])` above. ### page.stop() Unbind both the `popstate` and `click` handlers. ### page.base([path]) Get or set the base `path`. For example if page.js is operating within `/blog/*` set the base path to "/blog". ### page.exit(path, callback[, callback ...]) Defines an exit route mapping `path` to the given `callback(s)`. Exit routes are called when a page changes, using the context from the previous change. For example: ```js page('/sidebar', function(ctx, next) { sidebar.open = true next() }) page.exit('/sidebar', function(ctx, next) { sidebar.open = false next() }) ``` ### page.exit(callback) Equivalent to `page.exit('*', callback)`. ### Context Routes are passed `Context` objects, these may be used to share state, for example `ctx.user =`, as well as the history "state" `ctx.state` that the `pushState` API provides. #### Context#save() Saves the context using `replaceState()`. For example this is useful for caching HTML or other resources that were loaded for when a user presses "back". #### Context#handled If `true`, marks the context as handled to prevent [default 404 behaviour][404]. For example this is useful for the routes with interminate quantity of the callbacks. [404]: https://github.com/visionmedia/page.js#default-404-behaviour #### Context#canonicalPath Pathname including the "base" (if any) and query string "/admin/login?foo=bar". #### Context#path Pathname and query string "/login?foo=bar". #### Context#querystring Query string void of leading `?` such as "foo=bar", defaults to "". #### Context#pathname The pathname void of query string "/login". #### Context#state The `pushState` state object. #### Context#title The `pushState` title. ## Routing The router uses the same string-to-regexp conversion that Express does, so things like ":id", ":id?", and "*" work as you might expect. Another aspect that is much like Express is the ability to pass multiple callbacks. You can use this to your advantage to flatten nested callbacks, or simply to abstract components. ### Separating concerns For example suppose you have a route to _edit_ users, and a route to _view_ users. In both cases you need to load the user. One way to achieve this is with several callbacks as shown here: ```js page('/user/:user', load, show) page('/user/:user/edit', load, edit) ``` Using the `*` character we can alter this to match all routes prefixed with "/user" to achieve the same result: ```js page('/user/*', load) page('/user/:user', show) page('/user/:user/edit', edit) ``` Likewise `*` can be used as catch-alls after all routes acting as a 404 handler, before all routes, in-between and so on. For example: ```js page('/user/:user', load, show) page('*', function(){ $('body').text('Not found!') }) ``` ### Default 404 behaviour By default when a route is not matched, page.js invokes `page.stop()` to unbind itself, and proceed with redirecting to the location requested. This means you may use page.js with a multi-page application _without_ explicitly binding to certain links. ### Working with parameters and contexts Much like `request` and `response` objects are passed around in Express, page.js has a single "Context" object. Using the previous examples of `load` and `show` for a user, we can assign arbitrary properties to `ctx` to maintain state between callbacks. To build a `load` function that will load the user for subsequent routes you'll need to access the ":id" passed. You can do this with `ctx.params.NAME` much like Express: ```js function load(ctx, next){ var id = ctx.params.id } ``` Then perform some kind of action against the server, assigning the user to `ctx.user` for other routes to utilize. `next()` is then invoked to pass control to the following matching route in sequence, if any. ```js function load(ctx, next){ var id = ctx.params.id $.getJSON('/user/' + id + '.json', function(user){ ctx.user = user next() }) } ``` The "show" function might look something like this, however you may render templates or do anything you want. Note that here `next()` is _not_ invoked, because this is considered the "end point", and no routes will be matched until another link is clicked or `page(path)` is called. ```js function show(ctx){ $('body') .empty() .append('