# Routes and URI

Stratox Pilot can be used as a standalone library. As a universal library, it works across different platforms without needing any external dependencies. This independence makes Stratox Pilot a practical option for developers in search of a dependable routing tool that combines advanced features and modular design in a compact package.

You can find the router file at: `src/routes/app.js`

#### A Really Basic Example

```js
// Possible path: #about
router.get('/about', function({ http, services, helper, context }) {
});
```

You can, of course, add multiple paths:

```js
// Possible path: #about/contact
router.get('/about/contact', function({ http }) {
});
```

#### Using Regular Expressions

To incorporate regular expressions in routing patterns, enclose the expression within curly brackets: `{PATTERN}`. This syntax allows for flexible and powerful URL matching based on specified patterns.

```js
// Possible path: #about/location/stockholm
router.get('/about/location/{[a-z]+}', function({ http }) {
});
```

#### Binding Router Patterns to a Key

It is strongly advised to associate each URI path you wish to access with a specific key. This approach enhances the clarity and manageability of your route definitions.

```js
// Possible path: #about/location/stockholm
router.get('/{page:about}/location/{city:[^/]+}', function({ http }) {
    // http.vars.page[0] is expected to be "about"
    // http.vars.city[0] is expected to be any string value (stockholm, denmark, new-york) from passed URI.
});
```

You can also map an entire path to a key, allowing for more concise and organized route management.

```js
// Possible path: #about/contact
router.get('/{page:about/location}', function({ http }) {
    // http.vars.page[0] is expected to be "about"
    // http.vars.page[1] is expected to be "location"
});
```

#### Combining Pattern with Keywords

Combining patterns with keywords (e.g., `post-[0-9]+`) enables you to create more expressive and versatile route definitions.

```js
// Possible path: #articles/post-824/hello-world
router.get('/articles/{id:post-[0-9]+}/{slug:[^/]+}', function({ http }) {
    // http.vars.id[0] is expected to be "post-824"
    // http.vars.slug[0] is expected to be "hello-world"
});
```

#### Handling Unlimited Nested Paths

To accommodate an unlimited number of nested paths within your routing configuration, you can utilize the pattern `.+`. However, it's strongly advised to precede such a router pattern with a specific prefix to maintain clarity and structure, as demonstrated in the example below with the prefix `/shop`.

```js
// Example of accessing a single category: #shop/furniture
// Example of accessing multiple nested categories: #shop/furniture/sofas/chesterfield
router.get('/shop/{category:.+}', function({ http }) {
    // Retrieves the last category segment from the path
    const category = http.vars.category.pop();
    console.log(`The current category is: ${category}`);
});
```

This approach allows for the dynamic handling of deeply nested routes under a common parent path, offering flexibility in how URLs are structured and processed.

#### Optional URI Paths

To define one or more optional URI paths, enclose the path segment (excluding the slash) in brackets followed by a question mark, for example: `/(PATH_NAME)?`. This syntax allows for flexibility in route matching by making certain path segments non-mandatory.

```js
// Possible path: #articles
// Possible path: #articles/post-824/hello-world
router.get('/articles/({id:post-[0-9]+})?/({slug:[^/]+})?', function(http) {
});
```

It's important to note that you should not enclose the leading slash in brackets. The leading slash is automatically excluded from the pattern, ensuring the correct interpretation of the route.

#### Catch Status Errors

There is an optional and special router pattern that lets you catch HTTP status errors within a router.

```js
router.get('[STATUS_ERROR]', function({ http, services, helper, context }) {
    if (http.status === 404) {
        console.log("404 Page not found", http.status);
    } else {
        console.log("405 Method not allowed", http.status);
    }
});
```

This route can be used to handle specific error codes, such as `404 Not Found` or `405 Method Not Allowed`, providing a better user experience.
