Search code examples
javascriptnode-webkithapi.js

Running Hapi.js in Nodewekit


Has anyone run Hapi application inside Nodewebkit?

This is my package.json:{ "name": "nw", "version": "1.0.0", "description": "", "main": "index.html", "node-main": "nwindex.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "webkit": { "page-cache":false }, "author": "", "window": { "toolbar": true, "width": 800, "height": 500 }, "license": "ISC", "dependencies": { "jquery": "^2.1.4", "nw": "^0.12.2", "pretty-bytes": "^1.0.2" } }

This is my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>

</head>
<body>
<script type="text/javascript">  
   var gui = require('nw.gui');
   var win = gui.Window.get();

   var fs = require("fs");
   gui.App.clearCache();


  setTimeout (function () {
     window.location = 'http://localhost:3000';
  },1000)

And this is my nwindex.js (Getting started from official site):

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
    reply('Hello, world!');
    }
  });

  server.route({
   method: 'GET',
   path: '/{name}',
   handler: function (request, reply) {
       reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
    }
  });
  server.start((err) => {

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

Problem is when I start the NW, it instantly exists. I can see what is going on inside.

I have removed code line by line to see hwere is the problem, and I have narrowed it down to initial calling line:

const Hapi = require('hapi');

It seems that crashes the application. Any help is appreaciated. Nodewebkit version I use:

  • nw.js v0.12.3
  • io.js v1.2.0
  • Chromium 41.0.2272.76

After more tests, it seems this is maeking a problem:

server.start((err) => {

This => operator. How to byspass it?


Solution

  • What you are describing is the ES6 arrow function which isn't available in the version of node you are using. ES6 in Node.js

    server.start(function(err){
    
        if (err) {
           throw err;
        }
        console.log('Server running at:', server.info.uri);
    });