簡體   English   中英

當用戶點擊不同的URL時,nodejs +表示自動重定向

[英]nodejs + express automatic redirection when user hits different url

我已經使用nodejs和ejs制作了一個公告板,並部署在heroku上。 我的問題是,當打開網站時,(例如: https//cool-plains-2948.herokuapp.com/ )什么都沒有顯示,因為公告欄僅在URL為/ posts時顯示。

無論如何,當網站打開或用戶錯誤地點擊其他URL時,是否可以自動重定向到/ posts?

我知道如何使用$ urlRouterProvider.otherwise('/');在angularjs中做到這一點; 但是有后端的方法嗎? 使用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";
    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM