我想做什么:如果数据库中存在该 url,请使用静态页面模板,如果没有,则显示特定的页面模板。似乎想不通,怎么也...
我的应用.js文件
app.get('*', function(req, res){
var currenturl = req.url;
console.log('URL IS: ' + my_path)
if (!!db.get(my_path) )
{
//If it does exist in db
console.log('Does exist');
res.render('index', { thetitle: 'Express', title: db.get(currenturl).title, content: db.get(currenturl).content });
}else{
//If it doesn't exist in db
redirect to other sites
Like:
if you go to "/page" it will run this => app.get('/page', routes.index)
or "/users" will run => app.get('/users', routes.users)
}
});
您必须创建自己的简单中间件。只要确保把它放在express.router
上面
app.use(function(req, res, next){
if (!!db.get(my_path)) {
// render your site from db
} else {
// call next() to continue with your normal routes
next();
}
});
app.get('/existsInDB', function(req, res) {
// should be intercepted by the middleware
})
app.get('/page', function(req, res) {
// should not be intercepted
res.render('page')
})
使用 Express 很容易。 您可以使用redirect
函数:
if (url_exists) res.render('index');
else res.redirect('/foo/bar');