Serving static files with inert

inert (https://github.com/hapijs/inert) is the static file and directory handler module for hapi. It provides a lot of useful utilities for simplifying serving static content. Let's look at one of those now:

const Path = require('path');
const Hapi = require('hapi');
const Inert = require('inert');                    // [1]
const server = new Hapi.Server();
server.connection({ port: 3000 });
server.register(Inert, (err) => {                  // [2]
  server.route({
    method: 'GET',
    path: '/{param*}',
    handler: {                                     // [3]
      directory: {                                 // [3]
        path: Path.join(__dirname, 'public'),      // [3]
        listing: true                              // [3]
      }                                            // [3]
    }                                              // [3]
  });
  server.start((err) => {
    console.log(`Server running at: ${server.info.uri}`);
  });
});

Hopefully, this example is quite readable, and ...

Get Getting Started with hapi.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.