Skip to main content

Create controllers

Prep (start frourio in dev mode)

Before you create the directory and start writing the controller, start frourio in dev mode.

yarn dev:frourio
# or
yarn dev # all

Creation and automatic generation of directories

Now that we're ready, let's create a memo/ directory in server/api/.

Then three files, index.ts $relay.ts and the main one, controller.ts, are automatically generated under the directory we just created.


We just ran frourio in dev mode, and it does two things while it's running:

  • Auto-generate files when the directory is added to the server/api/
  • Auto-generate $server.ts if there are changes in server/api/

index.ts - API type definitions

export type Methods = {
get: {
resBody: string

Write the API request type definition.

$relay.ts - Bridge to the server framework

A bridge between Fastify and the controller. Files starting with $ are auto-generated, so you don't have to edit them.

controller.ts - The definition of controllers

import { defineController } from './$relay'

export default defineController(() => ({
get: () => ({ status: 200, body: 'Hello' })

This file defines the behavior of the controller that received the request.

The controller is defined in $server.ts by passing a function that returns response data for each HTTP method as an argument to defineController().

$server.ts excerpt:


We can see that route definitions are automatically generated.