question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Hapi.js?

See original GitHub issue

I’m getting an error when trying to use Nunjucks with Hapi.js.

Steps to reproduce:

  1. npm i hapi nunjucks -S

  2. Create a server.js file with the following contents:

    'use strict';
    
    var Hapi = require('hapi');
    var nunjucks = require('nunjucks');
    // var handlebars = require('handlebars');
    
    var serverOptions = {
    views: {
      engines: {
          html: nunjucks
      },
      path: 'views'
    }
    };
    
    var server = new Hapi.Server(3000, serverOptions);
    server.route({
    method: 'GET',
    path: '/',
    handler: function (res, reply) {
        reply.view('index', {
            title: 'Nice site',
            name: 'Roger',
            header: 'HEADERR',
            footer: 'FOOTERR'
      });
    }
    });
    server.start(function () {
    console.log('Hapi %s server running at %s', Hapi.version, server.info.uri);
    });
    
  3. Create a views/index.html with the following contents:

    <!doctype html>
    <html lang="en">
    <head>
    <title>{{ title }}</title>
    </head>
    <body>
    
    <div class="container">
      {{ header }}
    
      <header class="page-header">
        <h1>Hello {{ name }}, <small>Welcome back</small></h1>
      </header>
    
      {{ footer }}
    </div>
    
    </body>
    </html>
    
  4. $ npm start

  5. $ open http://localhost:3000

Actual results

$ node server
Hapi 6.9.0 server running at http://pdehaan.local:3000
Debug: hapi, internal, implementation, error
    TypeError: object is not a function: object is not a function
    at renderer (/Users/pdehaan/dev/github/hapi-examples/hapi-views-nunjucks/node_modules/hapi/node_modules/vision/lib/index.js:145:36)
    at /Users/pdehaan/dev/github/hapi-examples/hapi-views-nunjucks/node_modules/hapi/node_modules/vision/lib/index.js:294:13
    at /Users/pdehaan/dev/github/hapi-examples/hapi-views-nunjucks/node_modules/hapi/node_modules/vision/lib/index.js:411:20
    at Object.engine.compileFunc (/Users/pdehaan/dev/github/hapi-examples/hapi-views-nunjucks/node_modules/hapi/node_modules/vision/lib/index.js:154:24)
    at /Users/pdehaan/dev/github/hapi-examples/hapi-views-nunjucks/node_modules/hapi/node_modules/vision/lib/index.js:401:16
    at fs.js:271:14
    at Object.oncomplete (fs.js:107:15)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
seldocommented, Oct 19, 2014

I ran into this myself. The Nunjucks.compile() method does not export the handlebars-style interface expected by Hapi, but it can be trivially replicated using a combination of Nunjucks’ compile() and render() so I wrote a teeny-tiny module to do it:

https://www.npmjs.org/package/nunjucks-hapi

The following example works:

var Hapi = require('hapi')
var Path = require('path')
var Nunjucks = require('nunjucks-hapi')

var server = new Hapi.Server('localhost', 5000)

// set up templates
server.views({
  engines: {
    html: Nunjucks
  },
  path: Path.join(__dirname, 'views')
})

// Add a route
server.route({
  method: 'GET',
  path: '/test',
  handler: function (request, reply) {
    reply.view('mytemplate',{
      'myvariable': 'myvalue'
    })
  }
})

// start server
server.start()
0reactions
seldocommented, Oct 25, 2014

Hey Paulo - Glad you could get it working, could you tell me what about my module isn’t working for you? I double checked and added inheritance and includes to my example:

https://github.com/seldo/nunjucks-hapi/tree/master/test

And it seems to be working fine. [Edit: I had trouble getting your solution to work initially, deleted that.]

Your solution works too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Help - hapi.dev
Free support is always available on GitHub. Just open an issue with your question and a community member will try to help. For...
Read more >
hapi.dev - The simple, secure framework developers trust
hapi provides the right set of core APIs and extensible plugins to support the requirements of a modern service - session management, security,...
Read more >
hapi modules LTS support policy
The hapi core module and its dependencies are published under the following support rules: Every major version of the core module receives at...
Read more >
Plugins - hapi.dev
Plugins. There are dozens of plugins for hapi, ranging from documentation to authentication, and much more. If you wrote or use a plugin...
Read more >
Resources - hapi.dev
This handbook is a collection of tutorials for Hapi.js framework. You can find tutorials about validation, working with 3rd plugins, sending/getting data, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found