Mantra how-to guide

Understanding Mantra core middlewares

Core middlewares are neede in all Mantra projects. Undestanding them will allow you to set your owns correctly

13-Apr-2022
A  A  A 

Mantra core adds some middlewares by default.

Mantra middlewares are Express middlewares, because Mantra uses Express as its underlying web framework (if you don't know what they are, you can read about them at Express documentation).

You can list the middlewares in your project with de command "show-middlewares":

$ mantrad show-middlewares

In Mantra, each middleware has a weight: it is used to indicate the order in which they are executed when a new get or post request comes.

You can write your own middlewares in your projects easily. Read Mantra documentation about Mantra middlewares.

Here are the default Mantra middlewares:

  • sanitizedpath (weight -5000): this middleware adds to response object a property called "sanitizedPath" with the requested path (request.path) wihout the Mantra Instance ID (or miid), if it is included in the path.

  • setmantaapi (weight -1200): this middleware creates the main Mantra API object and adds it to response.MantraAPI property. Because Mantra API object encapsulates all framework functionality, by each request, Mantra expects you to use the same object to get a high performance.

  • checklanding (weight -180): checks if the current request is a "get" and if points to / or /index.html. If that is the case, then the root document or the landing view is rendered.

  • prerequest (weight -48): this middleware checks if the get or post request has a prerequest that should be checked.

  • accesscondition (weight -46): this middleware checks if the get or post request has accessconditions that should be checked.

The recommendation in Mantra when you develop your own middlewares, is to set a positive number for their weights.