Skip to content
Chris Manson edited this page Sep 20, 2016 · 2 revisions

All of express-autoroute-json is essentially based on middlewares. You will find more information about the way the middlewares work in the Data Flow page. For now let's focus on Pre and Post middelwares.

Intention

Pre-middleware happens before any work has been done with mongo but after authentication and authorisation. The intention of pre-middleware is so that you might alter the request's body before it is passed into the rest of the system e.g. to translate a parameter or add extra data that the frontend didn't know about like the logged in user's email.

Post-middleware happens after all work is done so the action has been committed to the database. For this reason the find action does not have a postMiddleware because it does not make sense to do anything after a find has already been sent to the user.

Post-middleware is also only run on a successful http request and will be skipped over in the event of an error.

Pre-middleware implementation

...

Post-middleware implementation

Post-middleware is a fully blown express middleware (see internal middleware types) and takes the format:

module.exports.autoroute = autorouteJson({
  model: Project,
  create: {
    postMiddleware: function(req, res, next) {
       // do your magic here
    }
  },
  find: {},
});

Note: the response has already been sent to the user so be careful not to send headers again.