Member-only story
Server-Side Development with Hapi.js — Static File Directory and Input Validation
3 min readFeb 20, 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.
Directory Handler Options
We can configure how static files are served with some options.
For instance, we can write:
const Hapi = require('@hapi/hapi');
const Path = require('path');const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0',
}); await server.register(require('@hapi/inert')); server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: Path.join(__dirname, 'public'),
index: ['pic.png']
}
}
}); await server.start();
console.log('Server running at:', server.info.uri);
};process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});init();
We set the index
option to set the file to serve if we don’t have any value set for the param
URL parameter.