【问题标题】:Removing a specfic mapped route in Node.js at runtime removes static mapping?在运行时删除 Node.js 中的特定映射路由会删除静态映射?
【发布时间】:2013-03-18 21:15:10
【问题描述】:

基于 answer to this question 的函数,我编写了这个函数来删除实时站点上的路由(使用 Express 和 Node)。

function deleteRoute(url) {


 for (var i = app.routes.get.length - 1; i >= 0; i--) {
   if (app.routes.get[i].path === "/" + url) {
     console.log(app.routes.get[i]);
     delete app.routes.get[i];
     console.log(app.routes.get)
   }
 }
}

但是,当我运行它时,它似乎也删除了到我所有静态页面的路由,这些页面在启动时声明如下:

 app.use(express.static(__dirname + '/components'));

我一直在努力解决这个问题,但似乎无法掌握它。有人可以帮忙吗?每当我在前后记录 app.routes.get 时,看起来操作都正确完成了。

具体来说,这是我在删除路由后重新加载任何静态页面时遇到的错误:

 TypeError: Cannot call method 'match' of undefined

这里是删除前的app.routes:

 { get: 
  [ { path: '/',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/\/?$/i,
   params: [] },
 { path: '/index.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/index\.html\/?$/i,
   params: [] },
 { path: '/how_it_works.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/how_it_works\.html\/?$/i,
   params: [] },
 { path: '/about.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/about\.html\/?$/i,
   params: [] },
 { path: '/contribute.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contribute\.html\/?$/i,
   params: [] },
 { path: '/contact.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contact\.html\/?$/i,
   params: [] },
 { path: '/a.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/a\.html\/?$/i,
   params: [] } ],
post: 
 [ { path: '/admin-save.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/admin-save\.json\/?$/i,
   params: [] },
 { path: '/page-edit.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/page-edit\.json\/?$/i,
   params: [] },
 { path: '/get-pages.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/get-pages\.json\/?$/i,
   params: [] },
 { path: '/admin-delete.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/admin-delete\.json\/?$/i,
   params: [] } ] }

之后是这样的:

{ get: 
 [ { path: '/',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/\/?$/i,
   params: [] },
 { path: '/index.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/index\.html\/?$/i,
   params: [] },
 { path: '/how_it_works.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/how_it_works\.html\/?$/i,
   params: [] },
 { path: '/about.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/about\.html\/?$/i,
   params: [] },
 { path: '/contribute.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contribute\.html\/?$/i,
   params: [] },
 { path: '/contact.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contact\.html\/?$/i,
   params: [] },
  ],
 post: 
  [ { path: '/admin-save.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/admin-save\.json\/?$/i,
   params: [] },
 { path: '/page-edit.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/page-edit\.json\/?$/i,
   params: [] },
 { path: '/get-pages.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/get-pages\.json\/?$/i,
   params: [] },
 { path: '/admin-delete.json',
   method: 'post',
   callbacks: [Object],
   keys: [],
   regexp: /^\/admin-delete\.json\/?$/i,
   params: [] } ] }

【问题讨论】:

  • 在调用deleteRoute() 时,您能做一个console.log(app.routes),以便我们可以处理一些事情吗?
  • @Brad 添加了!非常感谢您对此进行调查!
  • 任何关于为什么会发生这种情况的倾向?我很茫然,因为如果您只阅读日志,它似乎就可以工作!
  • 那个错误在哪里被抛出?如果没有堆栈跟踪,它就不是很有用。

标签: javascript node.js express


【解决方案1】:

delete 用于从对象中删除键,而不是用于从数组中删除条目。通过调用delete,您实际上是将该数组位置的值设置为undefined,因此Express 在查看路由时仍会尝试处理该路由。

注意您之前的输入:

 { path: '/contact.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contact\.html\/?$/i,
   params: [] },
 { path: '/a.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/a\.html\/?$/i,
   params: [] } ],

对比之后:

 { path: '/contact.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contact\.html\/?$/i,
   params: [] },
  ],

您删除了“a.html”路径,但请注意contact.html 对象之后仍有一个,。那是因为数组项还在,只是没有任何值。

您需要使用splice 删除条目。

function deleteRoute(url) {
  for (var i = app.routes.get.length - 1; i >= 0; i--) {
    if (app.routes.get[i].path === "/" + url) {
      app.routes.get.splice(i, 1);
    }
  }
}

您在问题中链接到的问题的第二个答案中也指出了这种方法。

【讨论】:

  • 这太完美了!我知道我错过了像这样的小东西,它把整个事情都搞砸了。你是救生员!
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 2018-07-08
  • 2016-01-29
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
相关资源
最近更新 更多