在运行时删除Node.js中的特定映射路由会删除静态映射? [英] Removing a specfic mapped route in Node.js at runtime removes static mapping?
问题描述
根据这个问题的回答我的功能,我写了这个功能来删除一个直播站点上的一条路线(使用Express和Node)。
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'));
我一直在摔跤一段时间,似乎没有抓住它。有人可以帮忙吗?每当我在之前和之后登录app.routes.get时,看起来操作都是正确的。
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
这是删除之前的app.routes:
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: [] } ] }
以下是:
{ 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
,您实际上将该数组位置的值设置为 undefined
,因此Express仍将尝试处理
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.
请注意您的输入:
{ 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: [] },
],
您删除了a.html路径,但请注意,仍然有一个,
之后的 contact.html
对象。这是因为数组条目仍然存在,它只是没有价值。
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.
您需要使用 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);
}
}
}
还指出了这种方法在你的问题链接的问题的第二个答案。
This method is also pointed out in the second answer of the question you linked to in your question.
这篇关于在运行时删除Node.js中的特定映射路由会删除静态映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!