简体   繁体   中英

Using data generated from a hook in sails.js

In sails.js, can a service use data or functions generated by a hook or by sails.config.bootstrap ? Or it's only the other way around?

EDIT: I was trying to add a hook to set up my rate limiter's parameters before sails lift , and then use this rate limiter from within the application.

The bootstrap file in config/bootstrap.js is intended mainly for setting up data, jobs, etc. that are specific to your app and may need to run only once. It's run after all of your models, services and hooks have already loaded, so it can rely on them.

You can use a hook method inside of a service method--you just can't use it to set up a service. So, this is okay:

// config/services/GoodService.js
module.exports = {
  someMethod: function() {
    var rateLimit = sails.hooks.someHook.getRateLimit();        
  }
};

This is not okay:

// config/services/BadService.js
var rateLimit = sails.hooks.someHook.getRateLimit();
module.exports = {
  someMethod: function() {...do something with rateLimit...}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM