http://www.geocities.com/my_page/image.jpgwould return the file
image.jpgfrom the folder
my_pageon the webserver located at
www.geocities.com. In a web application today, resources come from many other sources of data, like a database or a connected device. The job of a web application is to provide, create or modify a resource for a URL, wherever that resource might come from.
http://conduit.dart.com/about. Most people recognize that typing this URL into a browser would take them to our company's "About" page. In other words, our "About" page is a resource and the URL identifies it.
http), a host (
conduit.dart.com) and a path (
/about). The host specifies the computer responsible for providing the resource, the path identifies the resource and the scheme lets both the requester and the host know how they should exchange information.
https) and the host refers to a machine where the application is running. Therefore, once the application gets the request, it only cares about the remaining component: the path.
Requests to a
Controllerbased on the request path. This process is known as routing. When an application starts up, routes are registered in a subclass of
ApplicationChannel. Each registered route creates a new channel of
Controllers that will handle the request.
Router.route. This method takes a route specification - a
Stringwith some syntax rules that will match the path of a request. This registration occurs when an application first starts by overriding
ApplicationChannel.entryPoint. For example:
routeis the route specification string. This particular route matches the path
/users. That is, a request for the URL
http://myserver.com/userswill be handled by the
linkFunctionclosure. (Leading and trailing slashes are stripped out when routes are compiled, so including them has no effect, but it is good style to show a leading slash.)
/users/foohas two path segments:
foo. A route specification matches each segment of a path against each of its segments. The path and the route must also have the same number of segments. Thus, the route specification
/users/foowould match the path
/users/foo, but it would not match the paths
:). The name of the variable follows this colon. For example, consider the following route that declares a path variable named
/users/foo, etc. The value of
foo, respectively. This route won't match
/users/1can both be covered by a single route specification.
) around it. The brackets can go before or after slashes. For example, the following two syntaxes register a route that accepts both
/users/1identifies a single user, where
/usersidentifies all users. Optional segments are used to create better code structure by forwarding requests that deal with a specific type of resource to the same controller. Therefore, the code to handle one user or multiple users is written in the same place.
/a/b/c. It would not match
userIDto numbers only:
:userIDsegment. Note that capture groups and parentheses in general can't be included in a route's regular expression.
*). This token allows for any remaining request path to be matched, regardless of its contents or length. For example, the route specification
/users/*would match the following paths:
FileController- which reads a file from the filesystem - might have a route
/file/*. It uses everything after
/fileto figure out the path on the filesystem.
Request.path. When a
Requestis handled by a router, its
pathis set to an instance of this type. Controllers deeper in the channel access
Request.pathto help determine which resource the request is identifying. The
pathis an instance of
RequestPathcontains an map of
variables, where the key is path variable name and the value is the value of that variable in the request. For example, consider a route specification
/users/:id. When a request with path
/users/1is routed, the value
1is stored in this map for the key
Strings, since a request path is a
Controllers may parse path variables into types like
Routerwill return a
404 Not Foundif there is no matching route for the request. No linked controllers will handle the request. This behavior may be overridden by providing a closure to