admin管理员组文章数量:1334174
Currently I am trying to remove the route of a Node.js server application on runtime.
for (k in self.app.routes.get) {
if (self.app.routes.get[k].path + "" === route + "") {
delete self.app.routes.get[k];
break;
}
}
After this method is called there is no longer the route in the self.app.routes object. But after I try to access the currently removed route I get the following error:
TypeError: Cannot call method 'match' of undefined at Router.matchRequest (/var/lib/root/runtime/repo/node_modules/express/lib/router/index.js:205:17)
Due to the documentation of express.js this must be the correct way of doing it.
The app.routes object houses all of the routes defined mapped by the associated HTTP verb. This object may be used for introspection capabilities, for example Express uses this internally not only for routing but to provide default OPTIONS behaviour unless app.options() is used. Your application or framework may also remove routes by simply by removing them from this object.
Does any body know how to correctly remove a route on runtime in Node.js?
Thanks a lot!
Currently I am trying to remove the route of a Node.js server application on runtime.
for (k in self.app.routes.get) {
if (self.app.routes.get[k].path + "" === route + "") {
delete self.app.routes.get[k];
break;
}
}
After this method is called there is no longer the route in the self.app.routes object. But after I try to access the currently removed route I get the following error:
TypeError: Cannot call method 'match' of undefined at Router.matchRequest (/var/lib/root/runtime/repo/node_modules/express/lib/router/index.js:205:17)
Due to the documentation of express.js this must be the correct way of doing it.
The app.routes object houses all of the routes defined mapped by the associated HTTP verb. This object may be used for introspection capabilities, for example Express uses this internally not only for routing but to provide default OPTIONS behaviour unless app.options() is used. Your application or framework may also remove routes by simply by removing them from this object.
Does any body know how to correctly remove a route on runtime in Node.js?
Thanks a lot!
Share Improve this question asked Feb 22, 2013 at 15:27 Robert WeindlRobert Weindl 1,0921 gold badge10 silver badges30 bronze badges 2- Can you confirm your version of Express? – Brad Commented Feb 22, 2013 at 15:30
- Sorry for that. My version is: "3.1.0" – Robert Weindl Commented Feb 22, 2013 at 15:32
1 Answer
Reset to default 9The error you are getting is because the route is still there. delete
won't remove the element, it will only set the element as undefined
. To remove use splice(k,n) (from kth element ,remove n items)
for (k in self.app.routes.get) {
if (self.app.routes.get[k].path + "" === route + "") {
self.app.routes.get.splice(k,1);
break;
}
}
Still your route function should handle this (choose which path/url to accept), which would be better.
本文标签: javascriptNodejs Remove route while server is runningStack Overflow
版权声明:本文标题:javascript - Node.js Remove route while server is running - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742364440a2461003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论