Forums >> Programming >> Open Source >>
Trying Out the Express, Koa and Hapi Frameworks




Posted:
bvstone

Trying Out the Express, Koa and Hapi Frameworks

 
Trying Out the Express, Koa and Hapi Frameworks

When it comes to node.js I'm finding it's like an onion.  But, not in the sense of peeling back layers, but adding layers on top of node.js.

Take for example the many frameworks available.  Express seems to be the most popular.  Koa seems to be a close 2nd and Hapi, while not as popular now, seems to be the new up and comer.

What is a Framework?

Think of a framework as a big group of functions to make your life easier.  Just like in the RPG world we have the eRPG SDK and CGIDEV2 that make creating dynamic web applications with RPG super easy, node.js also has a few different packages, mentioned above, that also try to help make things easier (and, from what I've seen, seem to make everyone's web pages look the same as well).

What Does the Code Look Like for These Frameworks?

As of now I have only set up the basic "Hello World" applications for each of these frameworks.  Here they are:

Express (works fine):

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World from Express!');
});

app.listen(8080, '192.168.1.39', function () {
  console.log('Example app listening on port 8080!');
});

Koa (works fine):

var koa = require('koa');
var app = koa();

app.use(function *(){
  this.body = 'Hello World from KOA';
});

app.listen(8080, '192.168.1.39');

Hapi (doesn't work):

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ address: '192.168.1.39', port: 8080 });

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

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);
});

Do These Frameworks Work on the IBM i?

From what I can tell, Express and Koa (with an exception explained later) seem to work fine.  But, the version of Hapi I installed seems to require node v4 or higher.  Checking the version of node installed on the IBM i we're seeing it v0.12.9 right now.  Hopefully that will be updated soon.

With Hapi we just get the following error when trying to start the server:

bash-4.2$ node app.js
/nodejs/bvstone/hapi/app.js:3
const Hapi = require('hapi');
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Doing some searching I found this link that explains why:

https://github.com/hapijs/hapi/issues/2925

So, I guess for now Hapi is on hold for the IBM i.

With Koa we get this error:

bash-4.2$ node app.js
/nodejs/bvstone/koa/app.js:4
app.use(function *(){
                 ^
SyntaxError: Unexpected token *
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Again, a search was found to explain this error:

https://github.com/koajs/koa/issues/179

Adding the --harmony flag to the node command seems to let the server run just fine.  I have no idea what that flag does, though.  :)

Express seems to work fine without any special needs.

I haven't gotten far enough into these frameworks yet, but I imagine trying to write applications without using one would be like writing CGI applications with RPG without using something like the eRPG SDK or CGIDEV2... only calling the HTTP APIs.  Even with wrappers, that gets really ugly, really fast.  So, I'm going to put together a simple application and try it with Express and Koa to see how they differ.


Last edited 03/29/2016 at 08:58:57


Reply




Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).