Response

Learn how to use the response object to make and return responses.

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 ...