2

根据这个问题的答案,我编写了这个函数来删除实时站点上的路由(使用 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: [] } ] }
4

1 回答 1

6

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”路径,但请注意在对象之后仍有 a 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);
    }
  }
}

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

于 2013-03-19T03:40:30.003 回答