2013-02-22 64 views
3

当前我正试图在运行时删除一个 Node.js服务器应用程序的路由。Node.js在服务器运行时删除路由

for (k in self.app.routes.get) { 
    if (self.app.routes.get[k].path + "" === route + "") { 
    delete self.app.routes.get[k]; 
    break; 
    } 
} 

调用此方法后,不再有self.app.routes对象中的路由。但之后,我尝试访问当前删除路线,我得到以下错误:由于express.js的文档

TypeError: Cannot call method 'match' of undefined at Router.matchRequest (/var/lib/root/runtime/repo/node_modules/express/lib/router/index.js:205:17)

这一定是这样做的正确方法。

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.

是否有任何机构知道如何在Node.js中的运行时正确删除路由?

非常感谢!

+0

你能确认你的Express版本吗? – Brad 2013-02-22 15:30:20

+0

对不起。我的版本是:“3.1.0” – 2013-02-22 15:32:49

回答

8

你得到的错误是因为路线仍然存在。 delete不会删除元素,它只会将元素设置为undefined。要删除使用拼接(K,N)(从第k个元素,去掉n个项目)

for (k in self.app.routes.get) { 
    if (self.app.routes.get[k].path + "" === route + "") { 
    self.app.routes.get.splice(k,1); 
    break; 
    } 
} 

还是你的路由功能应对此进行处理(选择接受哪条路径/ URL),这将是更好的。

相关问题