# Routing
Routes enable the outside world to interact with your app via URLs. The most basic ExpressWebJs routes accept a URI and a closure, providing a very simple way of defining routes and behavior.
Routes are registered inside the Routes directory file.
# Basic Routing
The most basic route binding requires a URL and a closure:
Route.get('/', () => { 'Hello World' });
Copied!
The return value of the closure will be sent back to the client as a response.
# Controller Route
You can also bind a route to a controller using a controller@method signature:
Route.get('users', 'UserController@index');
Copied!
The above signature UserController@index refers to the App/Http/Controller/UserController.js index method.
# Sub Route Folder
To create a route folder to house a particular route group, use the command:
node maker make-route [ROUTE_NAME]
Copied!
# Available Route Methods
The router allows you to register routes that respond to any HTTP verb:
Route.all($uri, $callback); Route.get($uri, $callback); Route.post($uri, $callback); Route.put($uri, $callback); Route.patch($uri, $callback); Route.delete($uri, $callback);
Copied!
# Route Groups
Sometimes you may need to register some routes within a group. This allows you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route.
Route.group('/prefix', ()=>{ Route.get($uri, $callback); });
Copied!
# Route Parameters
You can define dynamic routes using route parameters. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:
Route.get('/user/:id','UserController@show');
Copied!
You may define as many route parameters as required by your route:
Route.get('/posts/:id/likes/:likesId','PostController@show');
Copied!
Since the hyphen (-) and the dot (.) are interpreted literally, they can be used along with route parameters for useful purposes.
Route.get('/flights/:from-:to','FlightController@show');
Copied!
# Optional parameters
Occasionally you may need to specify a route parameter that may not always be present in the URI. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route.get('/user/:name?','UserController@show');
Copied!
# Regular Expression:
To have more control over the exact string that can be matched by a route parameter, you can append a regular expression in parentheses (()):
Route.get('/user/:userId(\d+)','UserController@show');
Copied!