【问题标题】:nodejs + express automatic redirection when user hits different url当用户点击不同的网址时,nodejs + express 自动重定向
【发布时间】:2016-04-06 01:39:36
【问题描述】:

我使用 nodejs 和 ejs 制作了一个公告板,并部署在 heroku 上。 我的问题是,当网站打开时,(例如:https://cool-plains-2948.herokuapp.com/)什么都没有显示,因为公告板只在 url 为 /posts 时显示。

当网站打开或用户错误地点击其他网址时,是否有自动重定向到/posts?

我知道如何在 angularjs 中使用 $urlRouterProvider.otherwise('/'); 但是有没有办法在后端做?使用 nodejs?

编辑:我做了什么并且成功了:

app.use(function(req, res, next) {
  if(req.url === '/') {
    res.redirect('/posts');
  }
});//redirection

【问题讨论】:

    标签: node.js express ejs


    【解决方案1】:

    你可以使用*匹配所有路径

    app.use("*", function(req, res, next) {
        var path = req.path; // just example how it can be done
    
        if (path === "/") {
           // ...
           path = "/post";
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 2011-09-08
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      相关资源
      最近更新 更多