Removing a specfic mapped route in Node.js at runtime removes static mapping?

Basing my function off of the answer to this question, I wrote this function to delete a route on a live site (using Express and 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)
   }
 }
}

However, when I run this it also seems to delete the routing to all of my static pages, which are declared at startup like this:

 app.use(express.static(__dirname + '/components'));

I've been wrestling this for a while and can't seem to get a grip on it. Can anyone out there help? Whenever I log app.routes.get before and after, it looks like the operation is done correctly.

Specifically, this is the error I get when reloading any static page after the route is removed:

 TypeError: Cannot call method 'match' of undefined

Here is app.routes before the deletion:

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

Here is it after:

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

Answers:

Answer

delete is for removing keys from objects, not for removing entries from arrays. By calling delete, you are essentially setting the value of that array location to undefined, so Express will still try to process that route when it looks through the routes.

Note your input before:

 { 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: [] } ],

vs after:

 { path: '/contact.html',
   method: 'get',
   callbacks: [Object],
   keys: [],
   regexp: /^\/contact\.html\/?$/i,
   params: [] },
  ],

You erased the 'a.html' path, but note that there is still a , after the contact.html object. That is because the array entry is still there, it just has no value.

You need to use splice to remove the entries.

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

This method is also pointed out in the second answer of the question you linked to in your question.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.