1

I try to set a cookie on page load by extending the apostrophe-express module. But the console shows an error ... after successfully output req.cookies to it.

Part of module's index.js:

module.exports = {
  extend: 'apostrophe-express',
  // [...]
  middleware: [
    function(req, res, next) {
      console.log(req.cookies); // Successful output.
      return next(); // App crashes.
    }
  ]
};

Console output; user, app and cookie values replaced:

{ '[app].csrf': '[randomString]',
  '[app].sid':
   '[someId]' }
/Users/[user]/Node/[app]/node_modules/apostrophe/lib/modules/apostrophe-templates/index.js:608
        req.browserCall('apos.pageReadyWhenCalm($("body"));');
            ^

TypeError: req.browserCall is not a function
    at Object.self.renderPageForModule (/Users/[user]/Node/[app]/node_modules/apostrophe/lib/modules/apostrophe-templates/index.js:608:13)
    at /Users/[user]/Node/[app]/node_modules/apostrophe/lib/modules/apostrophe-module/index.js:349:31
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:726:13
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:52:16
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:264:21
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:44:16
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:723:17
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:167:37
    at /Users/[user]/Node/[app]/node_modules/apostrophe/index.js:128:16
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:52:16
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:264:21
    at /Users/[user]/Node/[app]/node_modules/async/lib/async.js:44:16
    at Immediate.<anonymous> (/Users/[user]/Node/[app]/node_modules/apostrophe/index.js:429:20)
    at runCallback (timers.js:696:18)
    at tryOnImmediate (timers.js:667:5)
    at processImmediate (timers.js:649:5)

What am I missing? This answer was my base for the middleware function.

Edit: The same happens by returning res.send('ok') after setting the cookie.

Carsten
  • 152
  • 1
  • 12
  • Okay, I removed the line to extend `apostrophe-express` and the error doesn't occur anymore, but the cookie does not want to be set. – Carsten Jun 22 '18 at 13:25
  • Where exactly are you setting the cookie? It looks like all you're doing is logging the cookies that are currently on the request, instead of adding your own ones. Also, you definitely don't want to use the extend line here. That implies that you're trying to create a new module that is based off of apostrophe-express, instead of modifying the existing apostrophe-express module. – Joseph Jun 22 '18 at 19:58
  • I answered my question. Figured it out. But one weird thing happens: After setting my cookie, an empty line appears under the cookies in Chrome dev tools. – Carsten Jun 23 '18 at 06:44

1 Answers1

2

To use express middleware the right way, the expressMiddleware property has to be set.

afterConstruct: function(self) {
  self.expressMiddleware = function(req, res, next) {
    res.cookie('cookieName', 'value');
    return next();
  };
}

This sets a cookie in the browser and continues with the next middleware.

expressMiddleware is a property that every module has. No need to extend apostrophe-express.

Please enhance this answer if I am wrong, there is more to say about this topic or a better way exists.

Carsten
  • 152
  • 1
  • 12