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