Access request headers from beforeSave Model Hook

后端 未结 5 875
执笔经年
执笔经年 2021-01-18 05:07

How can i access the details of the user who raise the request from a Model Hook

Comment.beforeSave =  function(next,com) {
//Want to add 2 more properties          


        
5条回答
  •  执笔经年
    2021-01-18 05:49

    Faced with the same problem, I used node expiremantal domain feature (that intended for error handling).

    Saving incoming request object:

    // -- Your pre-processing middleware here --
    app.use(function (req, res, next) {
      // create per request domain instance
      var domain = require('domain').create();
    
      // save request to domain, to make it accessible everywhere
      domain.req = req;
      domain.run(next);
    });
    

    Next inside model hook you have access to req object, which is created per each connection:

    process.domain.req 
    

    Also StrongLoop team added context propagation (based on continuation-local-storage), but it is not documented yet.

提交回复
热议问题