Menu Close

Server-Side Development with Hapi.js — Handling Accept Headers

Hapi.js is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Hapi.js.

Parse Accept Header

We can parse the Accept header with the @hapi/accept module.

For example, we can write:

const Path = require('path');
const Hapi = require('@hapi/hapi');
const Accept = require('@hapi/accept');

const init = async () => {
  const server = new Hapi.Server({
    port: 3000,
    host: '0.0.0.0',
    debug: { request: ['error'] }
  });

  server.route({
    method: 'GET',
    path: '/',
    handler(request, h) {
      const charset = Accept.charsets("iso-8859-5, unicode-1-1;q=0.8");
      return charset
    }
});

  await server.start();
  console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});
init();

We call Accept.charsets to return an array of encodings.

charset is [“iso-8859–5”,”unicode-1–1"] .

The Accept-Encoding header is checked to get the returned value

We can use this array in our requests handler to check the preferred encoding.

We can pass in a 2nd argument with the values we want to compare against the string in the first argument:

const Path = require('path');
const Hapi = require('@hapi/hapi');
const Accept = require('@hapi/accept');

const init = async () => {
  const server = new Hapi.Server({
    port: 3000,
    host: '0.0.0.0',
    debug: { request: ['error'] }
  });

  server.route({
    method: 'GET',
    path: '/',
    handler(request, h) {
      const encoding = Accept.encoding("gzip, deflate, sdch", ["deflate", "identity"]);
      return encoding
    }
});

  await server.start();
  console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});
init();

We have the deflate in the string in the first argument and 'deflate' in the 2nd argument, so it’ll return 'deflate' .

We can do the same comparisons for language.

For instance, we can write:

const Path = require('path');
const Hapi = require('@hapi/hapi');
const Accept = require('@hapi/accept');

const init = async () => {
  const server = new Hapi.Server({
    port: 3000,
    host: '0.0.0.0',
    debug: { request: ['error'] }
  });

  server.route({
    method: 'GET',
    path: '/',
    handler(request, h) {
      const language = Accept.language("en;q=0.7, en-GB;q=0.8");
      return language
    }
  });

  await server.start();
  console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});
init();

We pass in a string. Then it returns the string according to the value of the Accept-Language header.

Also, we can write:

const Path = require('path');
const Hapi = require('@hapi/hapi');
const Accept = require('@hapi/accept');

const init = async () => {
  const server = new Hapi.Server({
    port: 3000,
    host: '0.0.0.0',
    debug: { request: ['error'] }
  });

  server.route({
    method: 'GET',
    path: '/',
    handler(request, h) {
      const language = Accept.language("en;q=0.7, en-GB;q=0.8", ["en-gb"]);
      return language
    }
  });

  await server.start();
  console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});
init();

to do the comparison with the value in the array.

Since it has 'en-gb' as its only entry, this is returned.

Conclusion

We can parse and check the Accept-Encoding and Accept-Language header with the @hapi/accept module.

Posted in Hapi