Search code examples
gun

How to run Gun server with Hapi?


I follow this tutorial to create Gun server. But I need to do it with Hapi.

Now, I get the following error:

> node server.js

Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!
0.8 WARNING! Breaking changes, test that your app works before upgrading! The adapter interface has been upgraded (non-default storage and transport layers probably won't work). Also, `.path()` and `.not()` are outside core and now in 'lib/'.
WARNING! This `file.js` module for gun is intended for local development testing only!
/home/trex/dev/learn/gun/server/server.js:17
gun.wsp(server);
    ^

TypeError: gun.wsp is not a function
    at Object.<anonymous> (/home/trex/dev/learn/gun/server/server.js:17:5)

Server source code:

const Hapi = require('hapi');
const Gun = require('gun');

const gun = new Gun();
const server = new Hapi.Server();

server.connection({ port: 3000, host: 'localhost' });

server.ext('onRequest', () => gun.wsp.server);

gun.wsp(server);

server.start((err) => {

    if (err) {
        throw err;
    }
    console.log(`Server running at: ${server.info.uri}`);
});

What is wrong here?


Solution

  • The bug solved in gun 0.8.8 https://github.com/amark/gun/pull/423

    const Hapi  = require('hapi');
    const Inert = require('inert');
    const Gun   = require('../gun/');
    
    const server = new Hapi.Server;
    server.connection({ port: 8080 });
    server.connections.forEach(c => Gun({ web: c.listener, file: 'data.json' }));
    
    server.register(Inert, () => {});
    
    server.route({
      method: 'GET',
      path: '/{param*}',
      handler: {
        directory: {
          path: __dirname,
          redirectToSlash: true,
          index: true
        }
      }
    });
    
    server.start();