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);
    }
  }
}

この方法は、質問でリンクした質問の2番目の回答でも指摘されています。

于 2013-03-19T03:40:30.003 に答える