gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:51 24 4
gpt4 key购买 nike

我的功能基于 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 对象之后仍有一个,。那是因为数组条目仍然存在,只是没有值(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);
}
}
}

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

关于javascript - 在运行时删除 Node.js 中的特定映射路由会删除静态映射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com