Documentation
Support
Class core.core.routes
Tools to set up routing. The default routing is to go to whatever file matches the path of the url. However, this can be overridden to route every url to one file, make certain extensions inaccessible to the client, or set up different routing schemes for each application. URLs are referred to as keys. The paths to files served are referred to as values. Absolute paths start with a "/", other paths are relative. A route can also specify how to compute "paramaters" for its JXP. To do this, use the attachment parameter to add.
routes.wiki matches /wiki/
So, if you set:
routes.wiki = "/my/wiki/"
If someone goes to www.yoursite.com/wiki, the appserver will serve them the file /my/wiki/index.jxp
You can also set subroutes: routes.wiki.abc matches /wiki/abc
routes.wiki.search = "/wiki/doSearch"; is equivalent to routes.wiki.search = "doSearch";
Regular expressions can also be used: routes.add( /abc/ , "" ) routes.add( /abc(\d)/ , "/foo/$0/$1" )
Using attachment:
routes.add( /abc(\d)/ , "index", { names: ["number"] } );
// incoming requests for /abc1 will call index with request.number = "1"
routes.add( /abca{3,}/, "index", { extra: { lotsOfA : "yes" } } );
// incoming requests for /abcaaaaa will call index with
// request.lotsOfA = "yes"
Defined in: /data/corejs/master/core/routes.js.
| Field Attributes | Field Name and Description |
|---|---|
|
log <static> |
| Method Attributes | Method Name and Description |
|---|---|
|
|
add( string|RegExp key , string value , Object attachment )
|
|
string
|
apply( string uri , HTTPRequest request , HTTPResponse response , prefix )
Apply this routing to the given request and return the correct file.
Looks for the most specific first
|
|
|
create()
If no global "routes" is already defined, then create a new Routes object and
push it to the global scope as "routes"
|
|
|
|
|
string
|
find( Object submodule )
|
|
string|Object
|
getEnd( Object obj )
|
|
|
setDefault( string end , Object attachment )
|
Field Detail
<static>
log
Routes.log
Routes logging messages are handled by the logger "log.routes"
Method Detail
add( string|RegExp key , string value , Object attachment )
Add a routing pattern
- Parameters:
- string|RegExp key
- Route to match
- string value
- Path to which key should be routed
- Object attachment
- Fields:
- names
- An array. Add the matching pieces of the URI to a request with the form names[0]=match1, ..., names[n-1]=matchn
- extra
- An object to be added to the request
- Throws:
- Exception
- If key is not a string or RegExp
{string}
apply( string uri , HTTPRequest request , HTTPResponse response , prefix )
Apply this routing to the given request and return the correct file.
Looks for the most specific first. So, if you have defined a route matching "/x"
and one matching "/x/y/z", the handler for "/x/y/z" will catch a request for "/x/y/z/w/q".
- Parameters:
- string uri
- URI requested
- HTTPRequest request
- HTTPResponse response
- prefix
- Returns:
- string The path to the file to be served to the client.
create()
If no global "routes" is already defined, then create a new Routes object and
push it to the global scope as "routes". Return the new object.
If the global "routes" already exists then create a new Routes object and add
it as a subroutes of "routes". Return the new object. The path for the
subroutes is taken from the directory structure.
If a subroutes (or the routes object itself) already exists for this path,
it wil be overwritten.
- Returns:
- A new routes object
currentRoot()
Returns the root at which the last sub-routes took over
- Returns:
- The root at which the last sub-routes took over
{string}
find( Object submodule )
Finds the route to a submodule, if it exists.
- Parameters:
- Object submodule
- The submodule being searched for.
- Returns:
- string The path, if found, otherwise null.
{string|Object}
getEnd( Object obj )
If the object passed has a route end, return it, otherwise return the object itself.
- Parameters:
- Object obj
- Object to check.
- Returns:
- string|Object The route end.
setDefault( string end , Object attachment )
Route all requests to a given value
