# Web Routing

Web routes in the CyberOcean Framework are defined in the /api/routes.js file. This file is essential for mapping HTTP requests to specific controller actions or functions, facilitating the handling of different types of Web requests.

# Structure of Web Routes

The routes are defined in the public scope or inside the plugin's scope, for example to call a route defined as /test-route, you can use to methods:

  1. Public route: https://<your-domain>/p/test-route
  2. scoped usage: @PK/test-route, and @PK will be automatically replaced with the plugin key.

Routes can be defined using two primary methods:

  1. Controller Action Routes: Map a route to a specific action in a controller.
  2. Inline Function Routes: Define the logic for a route directly within the route configuration.

# 1. Controller Action Routes

These routes map specific endpoints to actions defined in controllers.

# Available Attributes:

  • method: HTTP method (e.g., "get", "post", "put", "delete").
  • route: URL path for the route.
  • action: The action in the controller to be invoked, formatted as "actionName@ControllerName".

# Example:

{
  method: "get",
  route: "/welcome",
  action: "welcomePage@MainController",
},

# 2. Inline Function Routes

Inline function routes allow you to define the logic of a route directly within the routes.js file.

# Example:

{
  method: "get",
  route: "/current-time",
  function: /* js */`
    const currentTime = new Date().toISOString();
    return res.send(`<h1>Current Time: ${currentTime}</h1>`);
  `,
},

# Example with Liquid template:

{
  method: "get",
  route: "/test",
  function: /* js */`
    // Prepare data
    var data = {
      title: "Hello World",
      template: "test",
    };

    // Prepare options
    var options = {
      ignore_layout: true,
      ignore_csp: false,
      custom_layout: null,
      return_html: false,
    };

    // Load liquid data
    await utils.loadLiquidData(req, res, data);

    // Render liquid template and send response
    return await utils.liquidRun(req, res, "test", data, options);

    /* To do not automatically send response, set `return_html` to `true`, and use `res.send(html)` to send response manually:
    // Render liquid template
    const html = await utils.liquidRun(req, res, "test", data, options);

    // Send response
    return res.send(html);
    */
  `,
},