Javascript node.js express路由数据库相关

Javascript node.js express路由数据库相关,javascript,node.js,Javascript,Node.js,我想做的是: 如果数据库中存在url,请使用静态页面模板,如果不存在,请显示特定的页面模板。似乎不明白,怎么也 我的app.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('D

我想做的是: 如果数据库中存在url,请使用静态页面模板,如果不存在,请显示特定的页面模板。似乎不明白,怎么也

我的app.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)
    }
 });
使用方便。您可以使用
重定向
功能:

if (url_exists) res.render('index');
else res.redirect('/foo/bar');

您必须创建自己的简单中间件。只需确保将其放在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')
})

我不确定这就是我要找的。我已经更新了我的OP帖子,希望让自己更清楚。这正是我想要的!谢谢