Controllers
npx foal generate controller my-controller
import { Context, Get, HttpResponseOK } from '@foal/core';
export class ProductController {
@Get('/products')
listProducts(ctx: Context) {
return new HttpResponseOK([]);
}
}
Description
Controllers are the front door of your application. They intercept all incoming requests and return the responses to the client.
The code of a controller should be concise. If necessary, controllers can delegate some tasks to services (usually the business logic).
Controller Architecture
A controller is simply a class of which some methods are responsible for a route. These methods must be decorated by one of theses decorators Get
, Post
, Patch
, Put
, Delete
, Head
or Options
. They may be asynchronous.
Example:
import { Get, HttpResponseOK, Post } from '@foal/core';
class MyController {
@Get('/foo')
foo() {
return new HttpResponseOK('I\'m listening to GET /foo requests.');
}
@Post('/bar')
bar() {
return new HttpResponseOK('I\'m listening to POST /bar requests.');
}
}
Controllers may have sub-controllers declared in the subControllers
property.
Example:
import { controller, Get, HttpResponseOK, Post } from '@foal/core';
class MySubController {
@Get('/foo')
foo() {
return new HttpResponseOK('I\'m listening to GET /barfoo/foo requests.');
}
}
class MyController {
subControllers = [
controller('/barfoo', MySubController)
]
@Post('/bar')
bar() {
return new HttpResponseOK('I\'m listening to POST /bar requests.');
}
}
The AppController
The AppController
is the main controller of your application. It is directly bound to the request handler. Every controller must be, directly or indirectly, a sub-controller of the AppController
.
Example:
import { controller, IAppController } from '@foal/core';
import { ApiController } from './controllers/api.controller';
export class AppController implements IAppController {
subControllers = [
controller('/api', ApiController)
];
}
Contexts & HTTP Requests
The Context
object
On every request, the controller method is called with a Context
object. This context is unique and specific to the request.
It has seven properties:
Name | Type | Description |
---|---|---|
request | Request | Gives information about the HTTP request. |
state | { [key: string]: any } = {} | Object which can be used to forward data accross several hooks (see Hooks). |
user | { [key: string]: any } |null | The current user (see Authentication). |
session | Session |null | The session object if you use sessions. |
files | FileList | A list of file paths or buffers if you uploaded files (see Upload and download files). |
controllerName | string | The name of the controller class. |
controllerMethodName | string | The name of the controller method. |
The types of the user
and state
properties are generic. You can override their types if needed:
import { Context, Get } from '@foal/core';
interface State {
foo: string;
}
interface User {
id: string;
}
export class ProductController {
@Get('/')
getProducts(ctx: Context<User, State>) {
// ...
}
}
HTTP Requests
The request
property is an ExpressJS request object. Its complete documentation can be consulted here. The below sections detail common use cases.
Read the Body
The request body is accessible with the body
attribute. Form data and JSON objects are automatically converted to JavaScript objects in FoalTS.
POST /products
{
"name": "milk"
}
import { Context, HttpResponseCreated, Post } from '@foal/core';
class AppController {
@Post('/products')
createProduct(ctx: Context) {
const body = ctx.request.body;
// Do something.
return new HttpResponseCreated();
}
}
Read Path Parameters
Path parameters are accessible with the params
attribute.
GET /products/3
import { Context, HttpResponseOK, Get } from '@foal/core';
class AppController {
@Get('/products/:id')
readProduct(ctx: Context) {
const productId = ctx.request.params.id;
// Do something.
return new HttpResponseOK();
}
}
Read Query Parameters
Query parameters are accessible with the query
attribute.
GET /products?limit=3
import { Context, HttpResponseOK, Get } from '@foal/core';
class AppController {
@Get('/products')
readProducts(ctx: Context) {
const limit = ctx.request.query.limit;
// Do something.
return new HttpResponseOK();
}
}
Read Headers
Headers are accessible with the get
method.
import { Context, HttpResponseOK, Get } from '@foal/core';
class AppController {
@Get('/')
index(ctx: Context) {
const token = ctx.request.get('Authorization');
// Do something.
return new HttpResponseOK();
}
}