Skip to main content

HTTP Responses

Creating Responses

Strings & Arrays & Objects

All routes and controllers should return a response to be sent back to the client. Formidable provides several different ways to return responses. The most basic response is returning a string from a route or controller. Formidable will automatically convert the string into a full HTTP response:

Route.get('/', (): string => 'Hello World!')

In addition to returning strings from your routes and controllers, you may also return arrays. Formidable will automatically convert the array into a JSON response:

Route.get('/', (): Array<string> => [ 'Donald', 'Luna' ]))

You may also return an object, Formidable will automatically convert the object into a JSON response:

Route.get('/', (): object => {
name: 'Luna'

Notice how we didn't include the return keyword, this is because Imba returns the last value/object by default.

Attaching Headers To Responses

You may use the setHeader method to attach a header to your response. The header method is part of the FormRequest instance:

request.setHeader('x-header', 'x-value')

You may also attach multiple headers by chaining the setHeader method:

.setHeader('x-header-1', 'x-value')
.setHeader('x-header-2', 'x-value')
.setHeader('x-header-3', 'x-value')

This can be done from Route actions, Middlewares and any other place where the FormRequest instance is present.


Redirect responses are instances of the Redirect class. To get started with a simple redirect, you may return Redirect instance anywhere in your application:

import { Redirect } from '@formidablejs/framework''posts/deleted')

Redirecting To Named Routes

You may also redirect back to a named route by using the route method:


If your route has parameters, you may pass them as the second argument to the route method:

Redirect.route('', {
id: 1