Skip to content

firstandthird/hapi-views

Repository files navigation

hapi-views

Build Status Coverage Status

hapi-views is a hapi plugin that lets you quickly create view routes. You use JSON (or YAML) to specify routes that can pull in dynamic data from any source, then use that data to render an HTML page.

Features

  • works with any Hapi-compatible view engine
  • define view routes with concise JSON config files instead of programming route handlers in filthy Javascript
  • specify dynamic JSON for your views with varson
  • pull in data with Hapi server methods

Usage

server.method('getUserInfo', async(id) => {
  const user = await fetch(id);
  return user;
});
await server.register({
  plugin: require('hapi-views'),
  options: {
    routes: {
      '/homepage/{userId}': {
        view: 'homepage',
        data: {
          title: "Your Homepage",
          amount: "{{ 15 + 25 }}",
          userInfo: "{{getUserInfo(request.params.userId)}}"
        }
      }
    }
  }
});
  • For each key in routes, hapi-views will register a route handler matching that path. The route will render the indicated view, passing data as the context of the view.
  • hapi-views uses the varson library to evaluate statements that appear in the double brackets. Any method defined in server.methods will be available from here and you can refer to the request object in the method parameters, eg: foo(request.params.userId) will work as expected from here.
  • passing '?debug=1' to a route will cause the server to log debug info as it renders the route

Route Options

  • view (required)

    The name of the view to render, the view must be available to the render engine you registered with hapi.

  • data (required)

    The data to pass to the rendering method

  • groupedData

    An object containing additional data you can pass to the rendering method

  • preProcess

    A method that takes in (request, options, h) as parameters and is called after the request is processed, but before the view is returned, you can use preProcess to do any preprocessing you need

  • preResponse

    A method that takes in a function that takes in (request, options, h) as parameters and is called after the view has been rendered, but before it is returned to the client

  • onError

    A method that takes in (err, h) as parameters and is called any time there is an error processing your route. If onError is defined then whatever value it returns will be passed back to the client.

Top-Level Options

  • globals

    An object that will be merged with the data field and passed to each render function.

  • allowDebugJson (default is false)

    When this is set to true, you can pass '?json=1' to a view route and it will return the render data for the route instead of rendering the view

Example

server.method('getUserInfo', async(id) => {
  const user = await db.users.findOne({ _id: id });
  return user;
});

await server.register({
  plugin: require('hapi-views'),
  options: {
    globals: {
      serverName: 'Bob the Server',
      db: 'http://55.55.55.5555:2121/bobDb'
    },
    routes: {
      '/homepage/{userId}': {
        view: 'homepage',
        data: {
          title: "Your Homepage",
          amount: "{{ 15 + 25 }}",
          userInfo: "{{getUserInfo(request.params.userId)}}"
        },
        preProcess: (request, options, h) => {
          request.server.log('Going to render the view');
        },
        preResponse: (request, options, h) => {
          request.server.log('View has been rendered!');
        },
        onError: (err, h) => { return 'There was an error processing your request' }
      }
    }
  }
});