Javascript.Prototype.find()无法正常工作。它与对象匹配,但是在匹配之后在 数组中返回值。
我有这个数组:
[
{ "src": "/api/health-check", "dest": "/api/health-check.js"},
{ "src": "/api/user", "dest": "/api/services/user" },
{ "src": "/api/auth", "methods": ["GET"], "dest": "/api/services/auth/index.js" },
{ "src": "/(.*)", "dest": "/app/$1" }
]
它通过了:
const route = routes.find(
({ src, methods }) => {
console.log("Does it match?", (!methods || methods.indexOf(req.method) !== -1) && req.url.match(new RegExp(`^${src.replace(/\(\?<[a-z]+>/, '(')}$`)))
return (
(!methods || methods.indexOf(req.method) !== -1) &&
req.url.match(new RegExp(`^${src.replace(/\(\?<[a-z]+>/, '(')}$`))
)
}
)
“匹配吗?” console.log()将每个路由记录为null,直到到达我想要的路由“ / api / auth”,它会记录匹配的路由,然后循环停止。它不会继续到下一个路由“(。*)”。
但是当我在find函数之后立即记录路由值时,这就是分配给路由变量的变量。
This is the found route { src: '/(.*)', dest: '/app/$1' }
好像find函数正在准确地找到匹配的路线,然后在比赛后给我路线。