微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-在运行时在Node.js中删除特定的映射路由会删除静态映射吗?

基于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: [] } ] }

解决方法:

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对象之后仍然有一个.那是因为数组项仍然存在,它没有任何价值.

您需要使用拼接来删除条目.

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐