Routing

Routing refers to the definition of application end points (URIs) and how they respond to client requests.

source:expressjs.com

Routes are created from Router common factory objects:

    const appManager = service.appManager;

    const router = appManager.get('app-name').buildRouter({
        version: 1.0,
        url: '/api/{version}/user' //{version} placeholder will get replaced by the actual router version
    });

The following is an example of very basic route:

    const route = router.buildRoute({
        type: 'get', //HTTP method type. One of the: `all`, `get`, `post`, `put`, `head`, `connect`, `options`, `delete`
        url : '/:username', //route endpoint can be also defined as a regular expression
        summary: 'Returns basics user data of a user with the :username',
        desc: 'Longer description of the endpoint'
    });

    route.main(function(req, res) {
        //pseudo-code
        return UserModel.fetch(req.params.username).then(function(user) {
            res.json(user);
        });
    });

The above example shows that the end points are defined on an Application object recevied from the AppManager.
The Application is an abstraction object which implements Application Interface.
bi-service comes with http(s) implementation of the ApplicationInterface using expressjs library under the hood,
however the interface implementation is not limited to HTTP protocol as anything that matches the request & response pattern (shell commands, message-queue protocols etc..) can share the same user-facing interface.
Practical examples of such usage are bi-service-shell & bi-service-rabbitmq plugins.

For more information on route definition, please see Router and Route APIs.

comments powered by Disqus