# 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";
});

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");

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:

 ts-node maker make-route [ROUTE_NAME]

# 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);

# Route Groups

Sometimes you may need to register some routes within a group. This allows you to share route attributes, such as URL segments and middleware, across a large number of routes without needing to define those attributes on each individual route.

Shared attributes are specified in an array format as the first parameter to the Route.group method.

# Middleware

Middleware are applied to all routes within the group by defining the list of middleware with the middleware parameter on the group attribute array. Middleware will be executed in the order you define this array:

Route.group({ middleware: ["auth", "admin"] }, () => {
  Route.get("/", (req: Request, res: Response) => {
    // Has auth and admin middleware
  });

  Route.get("user/profile", (req: Request, res: Response) => {
    // Has auth and admin middleware
  });
});

# Route Prefixing

A group of routes may be prefixed by using the prefix option in the attributes array of a group:

Route.group({ prefix: "admin" }, () => {
  Route.get("users", (req: Request, res: Response) => {
    // Matches The "/admin/users" URL
  });
});

# 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");

You may define as many route parameters as required by your route:

Route.get("/posts/:id/likes/:likesId", "PostController@show");

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");

# 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");

# 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");