ExpressJS路由器规范化/规范网址

Est*_*ask 8 javascript routing node.js express

我正在使用ExpressJS服务器对SPA进行规范化/规范化网址.

虽然它是由服务器端路由器备份的SPA - 模板可能与应用程序URL有所不同.其中一个区别是<link rel="canonical" href="https://example.com{{ originalPath }}">标签.不是相关的细节,而是解释了问题的背景.我希望只有一个URL响应200,其变体被重定向到301/302(适用于活人和搜索引擎).

我想使网址区分大小写和严格(没有额外的斜线),类似于Router选项,但非规范网址(在大小写或额外斜线上有所不同)应该将301/302重定向到规范网址而不是404.

在大多数应用程序中,我只想强制*路由的网址较低(查询除外),没有额外的斜杠.即app.all('*', ...),重定向是:

/Foo/Bar/ -> /foo/bar
/foo/Bar?Baz -> /foo/bar?Baz
Run Code Online (Sandbox Code Playgroud)

但是如果明确定义了路由,则可能会有例外情况.例如,有驼峰式路线:

possiblyNestedRouter.route('/somePath')...
possiblyNestedRouter.route('/anotherPath/:Param')...
Run Code Online (Sandbox Code Playgroud)

并且所有非规范网址都应该重定向到规范(参数大小写完整):

/somepath/ -> /somePath
/anotherpath/FOO -> /anotherPath/FOO
Run Code Online (Sandbox Code Playgroud)

规范网址背后的逻辑是非常简单的,所以我很难找到有关ExpressJS的这个主题的任何内容.

做这个的最好方式是什么?是否有中间件可以提供帮助?

小智 4

我寻找过 npm,但找不到任何,所以这让我抓狂,我express为每个请求编写了一个小任务,这似乎工作正常。请将其添加到您的代码中。

var urls = {
  '/main' : '/main',
  '/anotherMain' : '/anotherMain'
}

app.use(function(req, res, next){

  var index = req.url.lastIndexOf('/');

  //We are checking to see if there is an extra slash first
  if(req.url[index+1] == null || req.url[index+1] == undefined || req.url[index+1] == '/'){
     //slashes are wrong
     res.send("please enter a correct url");
     res.end();
  }else{

      for(var item in urls){
         if(req.url != item && req.url.toUpperCase() == item.toUpperCase()){
           res.redirect(item);
           console.log("redirected");
           //redirected
         }else if (req.url == item) {
           console.log("correct url");
           next();
         }else{
           //url doesn't exist
         }
     }

  }
  next();

});

app.get('/main', function(req, res){
  res.render('mainpage');
});

app.get('/anotherMain', function(req, res){
  res.send("here here");
});
Run Code Online (Sandbox Code Playgroud)

用法

您所要做的就是像上面那样将您的url添加到urls对象,并为其指定相同的键值。就是这样。看看它是多么容易。现在,您的所有客户端请求都将被重定向到正确的页面(区分大小写)

更新

我也给POST大家做了一个,我觉得还是比较准确的,你也试试吧。如果您希望在用户混淆斜杠时进行重定向,则需要regex为其编写一些内容。我没有时间,而且我的大脑也被炸了,所以我做了一个简单的。您可以随心所欲地更改它。每个网络结构都有自己的一套规则。

var urlsPOST = {
  '/upload' : '/upload'
}

app.use(function(req, res, next){

  if(req.method == 'POST'){

    var index = req.url.lastIndexOf('/');

    if(req.url[index+1] == null || req.url[index+1] == undefined || req.url[index+1] == '/'){

       //slashes are wrong
       res.sendStatus(400);
       res.end();
       return false;

    }else{

      for(var item in urlsPOST){
          if(req.url != item && req.url.toUpperCase() == item.toUpperCase()){
            res.redirect(307, item);
            res.end();
            return false;
            //redirected

          }else if (req.url == item) {
            console.log("correct url");
            next();

          }else{
            res.sendStatus(404).send("invalid URL");
            return false;
            //url doesn't exist
          }
      }
    }
  }
  next();
});
Run Code Online (Sandbox Code Playgroud)