ServerRouter

Server/Router.mjs

A standalone HTTP router that's based on the principle of middleware. Based on rill (see the npm package bearing the name), but stripped down and simplified.

new ServerRouter(verbose: Boolean)
Parameters
verbose (Boolean = false) Whether to be verbose and log a bunch of things to the console. Useful for debugging.
Instance Members
verbose
any(pathspec, action)
head(pathspec, action)
get(pathspec, action)
post(pathspec, action)
put(pathspec, action)
delete(pathspec, action)
options(pathspec, action)
on(methods, pathspec, action)
on_all(action)
onif(test, action)
handle(request, response)
iterate()

Contains context information about a single request / response pair.

new RouterContext(in_request: any, in_response: any)
Parameters
in_request (any)
in_response (any)
Instance Members
request
response
url
params
send
env

Helper methods for quickly sending responses to clients.

new Sender(response: http.ServerResponse)
Parameters
response (http.ServerResponse) The response object to use when sending requests.
Instance Members
html(status_code, template_filename, data)
plain(status_code, data)
redirect(status_code, new_path, message)

Holds request environment and state variables.

new RequestEnvironment(request: any)
Parameters
request (any)
Instance Members
logged_in
username
cookie
post_data

pathspec_to_regex

Shared/Pathspec.mjs

Converts a path specification into a regular expression. Originally from the server-side sibling of this (client-side) router.

pathspec_to_regex(pathspec: string, verbose: Boolean): RegExp
Parameters
pathspec (string) The path specification to convert.
verbose (Boolean = false) Optional. Whether to be verbose and log some stuff to the console. Default: false
Returns
RegExp: The resulting regular expression

ClientRouter

Client/Router.mjs

Client-side request router. You should use this in a browser. If you're not in a browser, you probably want ServerRouter instead.

new ClientRouter(options: object)

Extends EventEmitter

Parameters
options (object) The options object to use when creating the router.
Properties
verbose (Boolean) : Whether to be verbose and log debugging information to the console.
listen_pushstate (Boolean) : Whether to listen to the browser's pushstate event and automatically navigate on recieving it.
Instance Members
handle_popstates
add_404(callback)
add_page(routespec, callback)
navigate(path)
navigate_current_hash()