javascript - Dynamic dropping of handlers in restify -
context
i trying build dynamic server restify
(2.6.2) services installed , uninstalled once server started. realized can seen odd has sense in context of dsl
oriented project. achieve goal have implemented following functions:
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] }
the install function, installs handler in route specified path , method name [1]. uninstall function, uninstall handler dropping routes [2]. capabilities exposed services following code:
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); });
in post method [3], handler obtained body (it off-topic how undertaken) , service installed storing returned id in database. del method [4], retrieves id database , invokes uninstall function.
problem
this code has been unit-tested , works malfunction reached when try execute install/uninstall sequence following one. in example, please suppose body of requests contains same path
, http verb
, proper content build correct handler
:
/* post: /services : installed -> ok del: /services : resource not found -> ok post: /services : resource not found -> error :( */
in first install, handler
executed when resource acceded via path
, verb
. uninstall request correctly fulfilled because resource not found
message obtained when path
visited on verb
. nevertheless, when same body installed secondly in server, resource not found
returned when path
acceded on verb
.
i suppose error in [2] because, may be, not using correct unregister strategy restify
.
question
how can dropped handlers restify
once server started?
after looking @ restify source found following, may want try instead of 'delete' (https://github.com/restify/node-restify/blob/master/lib/server.js).
/* * removes route server. * pass in route 'blob' got mount call. * @public * @function rm * @throws {typeerror} on bad input. * @param {string} route route name. * @returns {boolean} true if route 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); };
Comments
Post a Comment