Modules

Modules (the main pieces of application functionality) can be snapped into place and used without affecting other code. They can be removed or disabled without causing any errors.

Modules are responsible for processing client-side requests so first of all you must to create a names of routes which will connect users with your business logic.

Let's create our first module

The first module that we will create is "users". For this we need go to the "src/modules" directory and create dir "users" there.

Then we need create required files:index.js and config.jsonthere.

index.js is system logic wrapper for your module. The minimal required inner code is:

module.exports = ({ ACTIONS, ROUTER, utils }) => {}; // we get default core features here

Here we can also importing config module:

const { routes } = require('./config.json');

As you can see we import key routes. Let's fill config file and add some route here:

{
  "name": "users",
  "version": "0.0.1",
  "routes": {
    "users_auth": { "path": "users/auth", "method": "get" }
  }
}

Then we must pass our routes to ROUTER and transform route name to valid ACTIONS event.

const { routes } = require('./config.json');

module.exports = ({ ACTIONS, ROUTER, utils }) => {
    ROUTER.set('routes', routes); // add module routes to global routes store

    const { users_auth } = utils.convertkeysToDots(routes); // get valid ACTIONS event name
};

And now we ready to handle client request from current route.

const { routes } = require('./config.json');

module.exports = ({ ACTIONS, ROUTER, utils }) => {
    ROUTER.set('routes', routes);

    const { users_auth } = utils.convertkeysToDots(routes); // get valid ACTIONS event name

    ACTIONS.on(users_auth, ({ headers, params, query, body }) => {
      return (body.name) ?
        Promise.resolve({ status: 'success' }) :
        Promise.reject({ error: { message: 'user name not exist!' } });
    });
};

Look good. But our module still separate from other application.

We can connect it to core just adding to array export in "src/modules/index.js"

const USERS = require('./users/index'); // require index file from our module

module.exports = () => [
  USERS, // export our module for connect it to core 
];

results matching ""

    No results matching ""