hapi — How to Set Response Status Code

Within previous tutorials throughout this hapi series, you’ve seen a lot functionality related to the server itself, requests and authentication. This guide is the first one geared towards responses and precisely shows you how to set the response status code that follows the RFC.

Before diving into the details, have a look at the series outline and find posts that match your interests and needs.

hapi Series Overview

Hapi Default: 200 OK

By default, hapi uses the 200 OK status for non-error responses that are created using the reply interface. Usually, that’s absolutely fine and your consuming clients will work properly. Nonetheless, there are situations where you want to provide more precise status codes, like when creating new documents (201 Created) within your database or in error scenarios. Let’s jump right into the topic and have a look on how to set your desired status code for responses.

Set Response Status Code

Using the reply interface with a usual JavaScript object will create a response that can be further customized. The interesting part is the .code(code) method that adjusts the response’s status.

hapi v17

server.route({  
  method: 'GET',
  path: '/',
  config: {
    handler: (request, h) => {
      const data = { key: 'value' }

      return h.response(data).code(201)
    }
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/',
  config: {
    handler: (request, reply) => {
      const data = { key: 'value' }

      reply(data).code(201)
    }
  }
})

Let’s assume a client sends a request with payload and you’ve created a new object within the database based on the client’s information. Now you want to indicate that the request was handled successfully by using the response status code 201 Created and as payload you’re sending the newly created object.

The code snippet above mainly illustrates the response creation with the help of reply and afterwards updates the status code to 201. Actually, that’s all you need to do when setting your status code of choice.

Error Status Codes with Boom

Hapi allows you to benefit from their library that creates HTTP-friendly error objects: called Boom. With the help of boom, you’re creating consistent error objects with correct HTTP status codes. Furthermore, you can add and send a custom error message with more specific information about the problem you’ve encountered while processing the request.

Using a boom error object as the response will automatically set the response status code. boom provides methods to easily create the response object for each HTTP status equal to 400 and above.

hapi v17

const Boom = require('boom')

server.route({  
  method: 'GET',
  path: '/',
  config: {
    handler: function (request, h) {
      throw Boom.notFound('Cannot find the requested page')
    }
  }
})

hapi v16

const Boom = require('boom')  
server.route({  
  method: 'GET',
  path: '/',
  config: {
    handler: function (request, reply) {
      reply(Boom.notFound('Cannot find the requested page'))
    }
  }
})

The example above leverages the .notFound(message) method of boom to create a 404 Not Found response. The custom message will be sent within the response payload. The response that is received on client side looks like this:

404 Not Found

{
  "statusCode": 404,
  "error": "Not Found",
  "message": "Cannot find the requested page"
}

The statusCode and error are available within the payload as well as your custom message.

Status Codes for Empty Response Payload

In situations where you don’t send any payload to your client, the 204 No Content status code is the correct choice. Actually, there are many clients (even browsers) that wait for status 200 OK in case the request was processed properly. That’s why hapi manages the balancing act of setting 200 OK as the default response status.

If you want to comply the RFC and send 204 No Content in situations where your response doesn’t contain any payload, configure your route to use the 204 status:

hapi v17

server.route({  
  method: 'GET',
  path: '/',
  config: {
    response: {
      emptyStatusCode: 204
    },
    handler: (request, h) => {}
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/',
  config: {
    response: {
      emptyStatusCode: 204
    },
    handler: function (request, reply) {
      reply()
    }
  }
})

The example above illustrates a response without any payload and a 204 No Content status code.

In case you provide data to reply, hapi will use the proper 200 or any (error) code you defined for the response. Of course, you can also use reply().code(204) to send empty responses with a precise status code and skip the extra route configuration.

Outlook

Hopefully this guide helps and motivates you to set proper status codes within your future coding sessions. Actually, clients will benefit a lot from precise responses including status codes other than just 200 and 400. Especially in error situations you’re able to display detailed messages on what needs to be fixed to successfully request a resource.

We appreciate feedback and love to help you if there’s a question on your mind. Let us know in the comments or on twitter @futurestud_io.

Make it rock & enjoy coding!


Additional Resources

Explore the Library

Find interesting tutorials and solutions for your problems.