Member-only story
Server-Side Development with Hapi.js — Content-Type and Crypto
3 min readFeb 23, 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.
Parsing Content-Type Header
We can parse the Content-Type
request header by using the @hapi/content
module.
For instance, we can do this by writing:
const Hapi = require('@hapi/hapi');
const Content = require('@hapi/content');const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
}); server.route({
method: 'GET',
path: '/',
handler(request, h) {
const type = Content.type('application/json; some=property; and="another"');
return type
}
}); await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We call Content.type
with the Content-Type
request header string to parse it.
Then we get:
{"mime":"application/json"}
as the value of type
.