Controllers
Controller are used to create endpoints and where most of you business logic will live.
Generate a controller with the Kult CLI
Run the kult
command and follow the prompts to create a new controller
bash
kult
Once a controller has been created the directory will have the following folder structure:
src/controllers
user.controller.ts
Manually Create a controller
To manually create a new controller create a new file in src/app/controllers
:
src/controllers/user.controller.ts
import { KultController, Get, Application, ControllerBase, Context } from '@kult/core';
@KultController('/users')
class UserController extends ControllerBase {
constructor(app: Application) {
super(app);
}
@Get('/find')
find(ctx: Context) {
return 'Hello World';
}
@Post('/update')
post(ctx: Context) {
return 'Hello World';
}
@Put('/create')
put(ctx: Context) {
return 'Hello World';
}
@Delete('/remove')
delete(ctx: Context) {
return 'Hello World';
}
}
export default UserController;
@KultController('/users')
Defines the base path of this controller, all the routes inside this controller will get a prefix of /users
.
@Get('/find')
creates a GET endpoint for/users/find
@Post('/update')
creates a POST endpoint for/users/update
@Put('/find')
creates a PUT endpoint for/users/create
@Delete('/find')
creates a DELETE endpoint for/users/remove
Once the controller gets instantiated a reference to the Application
object will passed to the constructor:
constructor(app: Application) {
super(app);
}
When controller function gets called the request Context
and a reference to the Application
object will passed to the function as parameters:
(ctx: Context, app: Application) => void