Skip to main content

Controller

A Controller is a class that contains methods also known as worker serving as endpoints or routes. Think of it as a section where different resources and services are available.

For example, in a real-world application designed for a university, you might have sections like StudentSection, TeacherSection, LibrarySection, etc.

Controller member

A controller has following members -

Creating controller

A Controller is a class that extends the Controller class from Fort.js.

Example

import { Controller } from "fortjs";

export class UserController extends Controller {

}

To make this controller active, you need to assign it to the routes of Fort. A controller is associated with a path.

import { Fort } from "fortjs";
import { UserController } from "./controllers";

Fort.routes = [{
controller: UserController,
path: "/user"
}];

In the code, we are adding our controller to the routes array along with a path. The path is used to associate the controller with a top-level route.

For example, if your website is abc.com, when a user hits the URL "abc.com/user", the UserController will be executed.

Note: You must have a method inside the controller with an HTTP route to allow the execution of the controller.