This forum has moved, please join us on github discussions. We will keep these old posts available for reference. Thank you!

Ohdotfive self.loader in 2.0.0

I can’t seem to find a reference to the old self.loader function which allowed us to work with req.extras, now req.data.

I tried creating a simple extension of ‘apostrophe-module’ using enableMiddleware. This is the code
var _ = require(‘lodash’);

module.exports = {
  extends:'apostrophe-module',
  construct:function(self,options){

    console.log("Construct ");
    self.enableMiddleware = function(){

      console.log("Enabling middlewares");
      self.apos.app.use(self.addDefaults);
    };

    self.addDefaults = function(req, res, next){

      console.log("running?");
      req.data.defaults = {"hello":"world"};
      return next();
    }


  }

};

(I don’t know if this is a howto question worthy of stackoverflow, apologies)

Is this the right way to handle this in 2.0.0?

This is a how-to question, really more of a stackoverflow thing, but no big deal…

Any module can have a pageServe method, like so:

self.pageServe = function(req, callback) { ... }

The callback is optional, if you don’t need to do anything async you can write:

self.pageServe = function(req) { ... }

This is invoked exactly the way the old loader methods were, and you can do the same things — mess with req.data, set req.data.page based on req.data.bestPage and req.data.remainder (but check out apostrophe-custom-pages for a simpler way to do that), etc.

Apostrophe has a number of callAll methods like this that are automatically invoked on all modules that happen to have them. A problem with the current documentation is that these methods are not documented in one central place, making it harder for you to find out about the technique. We’re working on that.

Thanks Tom I added both to stackoverflow