Create Services

Service is the smallest processing unit, used to handle a specific business logic

Hyron encourages you to build Microservice Architecture applications that bring great benefits to your application

Why ?

  • Easy to expand

  • Easy to understand

  • Easy to reuse


1. Initialization service

Using @hyron/cli
hyron init services
├── controller/ - contain processing logic for this service
├── model/ - contain contains data models, methods for communicating with the database
├── index.js - contains interfaces for use by other services
├── router.js - contains interfaces for use by Hyron to register to the service
├── appcfg.yaml - contains the configuration used for this service
├── package.json - contains the basic information of this service used for the npm registry
└── - contains descriptions for this service

2. Define controller

Hyron allows turning from a normal function to a router. Allows you to reuse better, and allows testing, extremely friendly with beginner

This allows you to easily switch from a normal controller to a router. Example

const serModel = require('../model/UserModel'); // mongoose model
module.export = class UserManager {
// create new user & save to mongo database
async createUser(name, age, location){
var newUser = await new UserModel({name, age, location});>{
// this is a global Error object of hyron that used to break flow to return to client a message with a status code
throw new HTTPMessage(
// return a object to response json data
return newUser;
const model = require('mongoose');
const UserModel = new model("user_info", {
name : String,
age : Number,
location : String
module.export = UserModel;


  • The processing functions used to register a router are also called executer

  • The executer input and output can be customized or processed before being transferred to the client using plugins

  • executer belongs to the logical layer, should only contain processing logic for a business

  • executer can share this variable with plugins

'this' scope

sharing mechanism 'this' variable allows communication between modules

Here are some of the default properties of this, check out HyronService for more info






A function handler, which can be used for plugins to use in analysis



The name of the event used to register the router


string | object

settings for this router, are declared in requestConfig



contains settings for this module, declared in appcfg.yaml file

3. Router definition

Hyron supports 2 types of services

  • HyronService : used for normal HTTP connection, or supported by addons from 3rd parties

  • UnofficialService : used for other special situations, such as sockets, high-level customization, or for another protocol


Let Hyron know that this is a service that can be supported by the http protocol, you need to return an interface specifically that the requestConfig contains descriptive information about that router. Example

const userModel = require('../model/UserModel'); // mongoose model
module.export = class UserManager {
static requestConfig(){
return {
createUser : {
method : "post"

Tips : If simple, you only need to declare the method directly instead of a description with an object. Example : createUser : "post"

The default param_parser plugins will help automate the process of passing variables from request to controller, saving you time and allowing you to package services more easily to be reused by other services.

Here are some of the attributes you should keep in mind of requestConfig





string | Array<string>

contains methods that can be used to register routers, including get, head, delete, post, put, patch, all, private . Default is get



Specifies which frontware plugins will be run or not for this method



Specifies which backware plugins will be run or not for this method



Specifies which plugins will be run or not for this method. It should be used in most cases



Customizing the path for this router. default is /prefix/module-name/method-name



customize dynamic path, which can be used as input for method. Example : /user/:name/age/:age



Specifying the function will be used to listen on this router. This method has a higher priority than mapping

Unofficial Services

Hyron supports a path that allows support for unofficial supported services or by addons from 3rd parties

var socket = require('');
module.exports = function (app, cfg) {
var io = socket(app);
io.on('connection', function(socket){
console.log('an user connected');

4. Declare & Routing

You should separate the interface from the hyron to a separate file, called router.js, containing the description of the router (if complicated).

In the simple case, just export the corresponding controller. Example

module.export = require('./controller/UserManager');
// Create an interface that makes it easy for other services to interact
const UserManager = require('./controller/UserManager');
module.export = new UserManager();

To use, you need to declare them in the JSON build file

"base_url" : "http://localhost:3000",
"services" : {
"user": "./services/user/router.js"

By default, the hyron will automatically register the url based on the declared information

  • To customize a path, use the path attribute to declare a router

  • You can also design rest-style routers with a combination of params

  • You can change the style of the url with style config

  • The above action is not recommended because it can affect the ability to share and reuse