str*_*ght 2 javascript node.js express
基于这个问题的答案我的功能,我写了这个函数来删除实时站点上的路由(使用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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,它似乎也删除了所有静态页面的路由,这些页面在启动时声明如下:
app.use(express.static(__dirname + '/components'));
Run Code Online (Sandbox Code Playgroud)
我已经冥想了一段时间,似乎无法控制它.任何人都可以帮忙吗?每当我在之前和之后记录app.routes.get时,看起来操作都是正确完成的.
具体来说,这是在删除路由后重新加载任何静态页面时出现的错误:
TypeError: Cannot call method 'match' of undefined
Run Code Online (Sandbox Code Playgroud)
这是删除前的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: [] } ] }
Run Code Online (Sandbox Code Playgroud)
以下是:
{ 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: [] } ] }
Run Code Online (Sandbox Code Playgroud)
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: [] } ],
Run Code Online (Sandbox Code Playgroud)
vs后:
{ path: '/contact.html',
method: 'get',
callbacks: [Object],
keys: [],
regexp: /^\/contact\.html\/?$/i,
params: [] },
],
Run Code Online (Sandbox Code Playgroud)
你删除了'a.html'路径,但请注意仍有一个,跟在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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您在问题中链接的问题的第二个答案中也指出了这种方法.
| 归档时间: |
|
| 查看次数: |
1861 次 |
| 最近记录: |