nodejs + 当用户点击不同时表示自动重定向 url

nodejs + express automatic redirection when user hits different url

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

有没有办法在网站打开或者用户误点击其他urls的时候自动重定向到/posts?

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

编辑:我所做的并且有效:

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

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

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

    if (path === "/") {
       // ...
       path = "/post";
    }

});