0
我想巩固我的整个快递API一堆route
使用的,而且我希望有一种方法,我可以做这样的事情:使用快速路由器匹配的路由
const app = express()
const get = {
fetchByHostname({
name
}) {
return `hey ${name}`
}
}
const map = {
'/public/hostname/:hostname': get.fetchByHostname
}
app.use((req, res, next) => {
const url = req.originalUrl
const args = { ...req.body, ...req.query }
const method = map[url] // this won't work
const result = method(args)
return res.json({
data: result
})
})
我m试图避免绕过req
和res
对象,只是在一个地方处理对客户端的响应。是否有Express/Node/.js模块或方法来匹配URL,就像我上面的map
对象一样?
我正在使用解构。在'args'对象中,我会在该方法内部访问'name'键。我最终提出了一个解决方案。 – benhowdle89