Hapi-Brick allows you to functionally split your Hapi server into Bricks.
A Brick is a Hapi plugin that includes a full MVC structure and increases the maintainability and the reusability of your Hapi development.
1. Install hapi-brick
npm install --save hapi-brick
2. Load the Brick Engine
server.register({register: require('hapi-brick')},function(err){// Handle errors});3. Create your Brick
Here is the required structure :
├── UserBrick
│ ├── index.js # Entry point of your Brick
│ ├── models # Folder of your models
│ │ └── User.model.js # A model of your Brick
│ └── routes # Folder of your routes
│ └── User.get.route.js # A route of your Brick
└── index.js # The Hapi server
The index.js describes the Brick and builds it from the Brick Engine :
'use strict';module.exports.register=function(server,options,next){// Retrieve the Brick Engine from the Hapi server objectvarBrick=server.plugins.Brick.Engine;// Create the current BrickvarUserBrick=newBrick(__dirname,options);// Register the current BrickUserBrick.register(server,options,next);};module.exports.register.attributes={// Name of the Brickname: 'UserBrick',// Version of the Brickversion: '1.0.0',// Dependencies of the Brick (you can add any Hapi plugin here)dependency: ['hapi-brick']};Model
A simple User.model.js mongoose model file :
'use strict';module.exports=function(mongoose){varSchema=mongoose.Schema;varUserSchema=newSchema({firstname: {type: String},lastname: {type: String}});varUserModel;if(mongoose.models.User){UserModel=mongoose.models.User;}else{UserModel=mongoose.model('User',UserSchema);}returnUserModel;};or a Sequelize model file :
"use strict";module.exports=function(sequelize,DataTypes){varUser=sequelize.define("Users",{id_user: {type: DataTypes.BIGINT,primaryKey: true,autoIncrement: true},firstname: {type: DataTypes.STRING(45)},lastname: {type: DataTypes.STRING(45)}},{});returnUser;};If you use Sequelize, your have to change the loader (see Full Brick options below).
Controller
A simple User.get.route.js route file :
'use strict';module.exports=[{method: 'GET',path: '/users',handler: function(request,reply){varusers=[{id_user: 1,fistname: "John",lastname: "Doe"}];returnreply(users);}}];4. Load your Brick
Finally load your Brick from its folder
server.register({register: require('./UserBrick')},function(err){// Handle errors});{
routes: {
loader: 'classic'
},
models: {
loader: 'mongoose|sequelize',
options: {
uri: '',
# Options for Mongoose or Sequelize connection
opts: {}
}
},
tests: {
loader: 'lab'
}
}
Mongoose options | Sequelize options
├── UserBrick
│ ├── index.js
│ ├── auth
│ │ └── JWT.auth.js
│ ├── models
│ │ └── User.model.js
│ ├── routes
│ │ ├── User.get.route.js
│ │ └── User.post.route.js
│ │ └── User.put.route.js
│ │ └── User.delete.route.js
│ ├── test
│ │ ├── User.create.spec.js
│ │ └── User.login.spec.js
│ │ └── User.update.spec.js
│ │ └── User.get.spec.js
│ │ └── User.delete.spec.js
│ └── views
│ └── validation.email.html
The Brick Engine simply load differents kind of file from their extension. Here are the available extensions :
- *.route.js : A route file
- *.model.js : A model file
- *.spec.js : A test file (soon)
- Add the tests handler
- Add some documentation
- Create a demo project
- Create a Yeoman generator
- Add the custom database handler
- Add the Auth handler