简体   繁体   中英

SailsJS - execute function after “sails lift”

I'm trying to use SailsJS to create a server (with no front-end) that will run some background processes. I was interested in the way SailsJS uses MVC and decided I will give it a try. I created some Models and Controllers, but the thing is that this server is not meant to listen anything. The idea would be to start the server and it should automatically start to run some operations. The problem is that I can't seem to find a way to run some sort of callback after "sails lift".

Maybe I'm making things more complicated with this approach. Do you have any suggestions on how to do it with SailsJs or maybe using another NodeJs alternative / setup?

Thanks.

If you want to run some code when Sails starts, you can use the config/bootstrap.js file, which is a module consisting of a function with a callback argument. From the docs :

module.exports.bootstrap = function (cb) {

  // It's very important to trigger this callback method when you are finished 
  // with the bootstrap!  (otherwise your server will never lift, since it's
  // waiting on the bootstrap)
  cb();
};

As stated in the comment, this happens before the server starts listening for connections, but that is probably fine for you since you don't care about connections anyway.

An alternative in Sails v0.10.x if it's important for your code to execute after Sails lifts and starts listening for connections is to bind a listener for the lifted event in the bootstrap:

module.exports.bootstrap = function (cb) {
  sails.on('lifted', function() {
     // Your post-lift startup code here
  });
  cb();
};

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