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 authMiddleware = rqeuire('./path/to/auth/middleware');
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.step(authMiddleware);
route.main(function(req, res) {
//pseudo-code
return UserModel.fetch(req.params.username).then(function(user) {
res.json(user);
});
});
Route
objects maintain a callback queue interanlly like express
does.
By calling route methods like validate
, step
or main
, middlewares are pushed into the queue.
Middlewares work with promises meaning middleware queue is assembled into promise chain internally.
For more information on route definition, please see Router
and Route
and Application
APIs.
Next Error management tutorial.
Internal guts notes:
The Application is an abstraction object which implements Application Interface
.
serviser
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 serviser-shell & serviser-rabbitmq plugins.