I_Debug_Everything
I_Debug_Everything

Reputation: 3816

Hapi.js application architecture

I've been working on a hapi.js application and have the following architecture:

Database
  --Models
    --usermodel.js
    --anothermodel.js
Routes
  --private
    --controllers
      --ctrl1.js
      --ctrl2.js
    --validators
      --validatr1.js
      --validtr2.js
  --public
    --controllers
      --ctrl1.js
      --ctrl2.js
test
  --dbtest.js
  --functiontest.js
server.js

We adopted this structure when working with express, but I'm starting with a new application and was wondering if there are resources where I can find a better application structure? I've looked into the offical website for hapi and other resources but couldn't find anything interesting.

Any help appreciated. Thanks.

Upvotes: 13

Views: 10219

Answers (2)

I_Debug_Everything
I_Debug_Everything

Reputation: 3816

I've been building a chat application and the following is what I've used:

  1. Group controllers/validators and routes of an entity into one folder.
  2. Group utilities inside common folder.
  3. Keep auth and related tasks in separate folder.
  4. Keep separate folder for bootstrap tasks.
  5. Group each folder using index.js that acts as a glue for all the files in the folder
  6. Add package.json for each folder to avoid relative path access.
  7. Use dotenv to load environment variables.

The modularity has been inspired by Angular style guidelines I've read on a few places, where emphasis is on grouping modules by entity instead of functionalities.

Following is my project architecture:

auth
 -- index.js
 -- package.json
modules
  --user
    --controller.js
    --validator.js
    --index.js
  --group
    --controller.js
    --validator.js
    --index.js
  --index.js
  --package.json
database
  --models
    --user.js
    --group.js
  --index.js
  --package.json
bootstrap
 --users.js
  --groups.js
  --index.js
  --package.json
test
  --user
    --model.js
    --route.js
    --index.js
  --index.js
  --package.json
app.js 

Upvotes: 2

Matt Harrison
Matt Harrison

Reputation: 13567

The good thing about hapi is that it doesn't dictate to you how you should structure your apps. It's up to you, for whatever makes sense in your life. You could throw everything you have in a single index.js file and still use all of hapi features, but you're probably going to have a hard time reading/maintaining that hairball later.

If that structure above, that you've used before, still makes sense to your application, there's absolutely nothing stopping you using the same or a similar structure.

models - just non-hapi specific node modules that talk to your db

exports.getUser = function (id, callback) { 

    Db.get('users', id, callback) 
};

controllers - modules that export route handlers

var User = require('../models/user');

exports.showUserPage = function (request, reply) {

    User.getUser(request.params.id, function (err, user) {

        if (err) {
            throw err;
        }

        if (!user) {
            return reply('User not found').code(404)
        }

        reply.view('user', user);
    });
}

validators - modules that export joi schemas

exports.showUserPage = {
    params: {
        id: Joi.number().required()
    }
} 

server.js - where you glue all of that together

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 7843 });

server.route({
    method: 'GET',
    path: '/users/{id}',
    handler: require('./controller/users').showUserPage,
    config: {
        validate: require('./validators/users').showUserPage
    }
});

server.start();

Plugins

You should probably take advantage of plugins, they let you split your application into logical chunks. You can still use the above structure but put it inside a plugin. New features you add later can go inside another plugin if they're unrelated (analytics, store etc). This lets you build a microservice architecture, where it's easy to scale out only the specific parts of your applications that need scaling.

What are other people doing?

If you're looking to change it just because you can, take a look at a few projects already built with hapi, and see how they're doing it:

Upvotes: 7

Related Questions