node.js - SailsJS - 在 "sails lift"之后执行函数

标签 node.js sails.js

我正在尝试使用 SailsJS 创建一个将运行一些后台进程的服务器(没有前端)。我对 SailsJS 使用 MVC 的方式很感兴趣,并决定尝试一下。我创建了一些模型和 Controller ,但问题是这个服务器并不打算听任何东西。 这个想法是启动服务器,它应该自动开始运行一些操作。问题是我似乎无法找到一种方法来在“扬 sails ”之后运行某种回调。

也许我用这种方法让事情变得更复杂了。您对如何使用 SailsJs 或使用其他 NodeJs 替代方案/设置有任何建议吗?

谢谢。

最佳答案

如果您想在 Sails 启动时运行一些代码,您可以使用 config/bootstrap.js 文件,这是一个由带有回调参数的函数组成的模块。来自 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();
};

如评论中所述,这发生在服务器开始监听连接之前,但这对您来说可能没问题,因为您根本不关心连接。

如果在 Sails 提升并开始监听连接后执行代码很重要,那么 Sails v0.10.x 中的替代方法是为 lifted 绑定(bind)一个监听器 Bootstrap 中的事件:

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

关于node.js - SailsJS - 在 "sails lift"之后执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039748/

相关文章:

node.js - 如何在mongodb中过滤两次之间的数据

javascript - Bluebird - 混合each() 和all()?

javascript - Sails.js index.ejs Controller 逻辑

node.js - 在 Node http-proxy 后面 sails

mongodb - 当模型验证失败时,Sails 不会显示错误

node.js - 原始 IP 未传递到 AWS Elastic Beanstalk 上单个容器 Docker 镜像上的容器

javascript - 使用 Node 加密和解密字符串

node.js - SequelizeDatabaseError : column "id_strain" of relation "strain" is already an identity column

node.js - 将 SailsJS 部署到 Heroku

javascript - 使用 sailsjs Passport 身份验证本地策略既不会给出任何错误,也不会对用户进行身份验证