dynamic删除处理程序在restify

上下文

我正在尝试使用restify (2.6.2)构build一个dynamic服务器,服务器启动后将安装和卸载服务。 我意识到这可以被看作是一个奇怪的东西,但它在一个面向DSL的项目的背景下是有意义的。 为了实现这个目标,我实现了以下function:

 var install = function (path, method, handler) { var id = server[method](path, function (request, response) { // [1] handler (request, response); }); return id; } var uninstall = function (id) { delete server.routes[id]; // [2] } 

安装函数在由path和方法名称[1]指定的路由中安装处理程序。 卸载函数,通过从路由中删除处理程序来卸载处理程序[2]。 以下代码将此function公开为服务:

 var db = ... var server = restify.createServer () .use (restify.bodyParser ({ mapParams: false })) .use (restify.queryParser ()) .use (restify.fullResponse ()); service.post ('/services', function (request, response) { var path = request.body.path; var method = request.body.method; var handler = createHandler (request.body.dsl) // off-topic var id = install (path, method, handler) db.save (path, method, id); // [3] }); service.del ('/services', function (request, response) { var path = request.body.path; var method = request.body.method; var id = db.load (path, method); // [4] uninstall (id); }); 

在post方法[3]中,一个处理程序是从正文中获取的(这是无关紧要的,这是如何进行的),并且安装了一个服务,将返回的id存储在数据库中。 del方法[4]从数据库中检索id并调用卸载函数。

问题

这个代码已经过了unit testing,并且正常工作,但是当我尝试执行像下面这样的安装/卸载序列时,出现故障。 在这个例子中,请假设所有请求的主体包含相同的path ,http verb和适当的内容来构build正确的handler

 /* post: /services : Installed -> ok del: /services : Resource not found -> ok post: /services : Resource not found -> Error :( */ 

在第一次安装时, handler在通过pathverbjoin资源时执行。 由于在verb上访问path时会获得Resource not found消息,所以正确履行了卸载请求。 尽pipe如此,当在服务器中安装了相同的主体时,如果在verb上joinpath则返回Resource not found

我想这个错误在[2]中,因为可能是,我没有使用正确的取消注册策略。

一旦服务器启动,如何有效地降低处理程序的restify

看到restify源代码后,我发现了下面这个,你可能想尝试一下,而不是简单的'删除'( https://github.com/restify/node-restify/blob/master/lib/server.js )。

 /* * Removes a route from the server. * You pass in the route 'blob' you got from a mount call. * @public * @function rm * @throws {TypeError} on bad input. * @param {String} route the route name. * @returns {Boolean} true if route was removed, false if not. */ Server.prototype.rm = function rm(route) { var r = this.router.unmount(route); if (r && this.routes[r]) { delete this.routes[r]; } return (r); };