Controllers are the heart of your application, responsible for handling incoming requests, processing data, and returning responses. In this section, we will explore how to create and use controllers in your application.
🔗Create a Controller
You can create a new controller by using either newController macro, or the short-version ctrl template. Both serve the same purpose. Here is an example of how to create a controller using the newController macro:
newController getHomepage:
respond("Welcome to the homepage!")Basically, the newController macro will transform into the following proc
proc getHomepage(req: var Request, res: var Response) {.gcsafe.} =
respond("Welcome to the homepage!")
# or, if you prefer the short version 👇
ctrl getHomepage:
respond("Welcome to the homepage!")There are multiple ways of sending a response from a controller. The respond template is a convenient way to send a text or HTML response. You can also use the json template to send a JSON response. The json template can take any Nim type and convert to JSON (using openparser/json under the hood):
type
User = object
name: string
ctrl getHomepage:
var user = User(name: "George Lemon")
json(user) # {"name": "George Lemon"}🔗Controllers location
Controllers are defined in the src/controller directory, and they are automatically loaded by the application.
🔗Controller linking
The ident name of a controller is always linked to its route path. For example, a controller named getHomepage will be linked to the route path get "/", and a controller named getUserProfile will be linked to the route path get "/user/profile".
For more information on how routes are linked to controllers, please refer to the Routing section.