最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js Remove route while server is running - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

The 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.

发布评论

评论列表(0)

  1. 暂无评论