使用ApostropheCMS路由中间件,并基于请求的路径参数,决定要服务的页面。代码示例如下:
//app.js
const apos = require('apostrophe')({
shortName: 'my-project',
modules: {
// ... other modules ...
'apostrophe-express': {
middleware: [
{
// middleware logic to serve different pages based on route
route: '/my-custom-page/:slug',
// If the middleware's `defer` property is true,
// this can be a promise.
handler: (req, res, next) => {
const { slug } = req.params
const page = slug === 'foo' ? 'foo' : 'bar'
return req.apos.templates.render(
res,
`my-custom-page-${page}`,
{ myData: 'data to pass to the template' }
)
}
}
]
}
}
})
这将捕获带有/my-custom-page/:slug
路由的所有请求,并在中间件中决定要服务的不同页面。在上面的示例中,如果路由参数 slug
等于“foo”,则服务器将服务“my-custom-page-foo”的模板;如果slug
等于“bar”,则服务器将服务“my-custom-page-bar”的模板。