我想在运行sailsjs时动态添加/删除路由。是什么方式?
我找到了一个添加路线的功能:
sails.router.bind(path, target);
它会起作用但是当我解除绑定时,它不起作用。
sails.router.unbind(???);
答案 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;
};