Member-only story
Server-Side Development with Hapi.js — Handling Accept Headers
3 min readFeb 21, 2021
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