この質問への回答に基づいて関数を作成し、ライブサイトのルートを削除する関数を作成しました(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: [] } ] }