Search⌘ K

Response

Explore how to work with HTTP responses in AdonisJs, including returning different content types, managing headers, sending JSON data, handling redirects, cookies, and enabling file downloads. Understand the response object's role in route handlers and middleware for building full-stack web applications.

We'll cover the following...

When a user requests something, we return a response. A response can be in JSON, HTML, or any other format. We can think of a request as input and response as the output of the application. AdonisJs passes the current HTTP response object as a part of the HTTP context and sends the HTTP context object to all route handlers and middleware.

Returning a response

'use strict'

class ConvertEmptyStringsToNull {
  async handle ({ request }, next) {
    if (Object.keys(request.body).length) {
      request.body = Object.assign(
        ...Object.keys(request.body).map(key => ({
          [key]: request.body[key] !== '' ? request.body[key] : null
        }))
      )
    }

    await next()
  }
}

module.exports = ConvertEmptyStringsToNull

Press Run and wait for the output to be ...