One common case for serving static content is setting up a file server. The following example shows how to setup a basic file serve in hapi:
const Path = require('path');
const Hapi = require('@hapi/hapi');
const Inert = require('@hapi/inert');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
routes: {
files: {
relativeTo: Path.join(__dirname, 'public')
await server.register(Inert);
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true
await server.start();
console.log('Server running at:', server.info.uri);
init();
The first thing you do is require both inert
and path
. As you will see, you will need both of these to get our file server up and running.