Member-only story

Server-Side Development with Hapi.js — Rendering Views

John Au-Yeung
3 min readFeb 21, 2021

--

Photo by Vinicius Wiesehofer on Unsplash

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.

Rendering a View

We can render a view with the h.view method.

For example, we can write:

index.js

const Path = require('path');
const Hapi = require('@hapi/hapi');
const Hoek = require('@hapi/hoek');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0',
debug: { request: ['error'] }
});
await server.register(require('@hapi/vision'));
server.views({
engines: {
html: {
module: require('handlebars'),
compileMode: 'sync'
},
},
relativeTo: __dirname,
path: 'templates',
});
server.route({
method: 'GET',
path: '/',
handler (request, h) {
return h.view('index');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();

templates/index.html

<div>Content</div>

--

--

No responses yet