在我的快递应用中,我想要以下路线。
/regions
/regions/europe
/regions/europe/france
/regions/europe/france/paris
我目前正在使用以下设置但是想知道是否有更简洁的方法?例如,您可以在一台服务器中处理这一切吗?
目前,如果我只有最后一部分/regions/:region/:country/:city
并转到/regions/europe/france
,我会找不到404页?
server.get('/regions/:region/', (req, res) => {
const actualPage = '/regions'
const queryParams = {
region: req.params.region
}
app.render(req, res, actualPage, queryParams)
})
server.get('/regions/:region/:country', (req, res) => {
const actualPage = '/regions'
const queryParams = {
region: req.params.region,
country: req.params.country
}
app.render(req, res, actualPage, queryParams)
})
server.get('/regions/:region/:country/:city', (req, res) => {
const actualPage = '/regions'
const queryParams = {
region: req.params.region,
country: req.params.country,
city: req.params.country
}
app.render(req, res, actualPage, queryParams)
})
答案 0 :(得分:1)
这应该是非常相同的:
const handleRegion = (req, res) => {
app.render(req, res, '/regions', req.params)
}
server.get('/regions/:region/', handleRegion)
server.get('/regions/:region/:country', handleRegion)
server.get('/regions/:region/:country/:city', handleRegion)
答案 1 :(得分:1)
您可以将一系列路由传递给get方法,这是一个示例:
server.get([
'/regions/:region',
'/regions/:region/:country',
'/regions/:region/:country/:city'
], (req, res) => {
const actualPage = '/regions'
const queryParams = req.params
res.render(req, res, actualPage, queryParams)
})