sails.js动态绑定和解除绑定路由

时间:2015-04-29 18:48:07

标签: routes sails.js router

我想在运行sailsjs时动态添加/删除路由。是什么方式?

我找到了一个添加路线的功能:

sails.router.bind(path, target);

它会起作用但是当我解除绑定时,它不起作用。

sails.router.unbind(???);

1 个答案:

答案 0 :(得分:2)

其路线对象包括方法路径

源代码如下

Router.prototype.unbind = function(route) {

  var sails = this.sails;

  // Inform attached servers that route should be unbound
  sails.emit('router:unbind', route);

  // Remove route in internal router
  var newRoutes = [];
  _.each(this._privateRouter.routes[route.method], function(expressRoute) {
    if (expressRoute.path != route.path) {
      newRoutes.push(expressRoute);
    }
  });
  this._privateRouter.routes[route.method] = newRoutes;

};