ExpressJS router normalized/canonical urls ExpressJS router normalized/canonical urls node.js node.js

ExpressJS router normalized/canonical urls


I have looked for npms, I could not find any, so this scratched my mind and I've coded a small task for express to do for each request, which seem to work fine. Please add this to your code.

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");});

USAGE

All you have to do is, add your urls to urls object like done above with giving it the same key value. That's it. See how easy it is. Now all of your clients request will be redirected to the correct page case sensitively.

UPDATE

I have also made one for POST requests, I think it is pretty accurate, you should also give it a try. Ff you want a redirect when the user mixes up slashes, you need to write some regex for it. I didn't have time also my brain was fried so I made a simple one. You can change it however you like. Every web structure has its own set of rules.

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();});


You probably want to write your own middleware for this, something along the lines of this:

app.set('case sensitive routing', true);/* all existing routes here */app.use(function(req, res, next) {  var url = find_correct_url(req.url); // special urls only  if(url){    res.redirect(url); // redirect to special url  }else if(req.url.toLowerCase() !=== req.url){    res.redirect(req.url.toLowerCase()); // lets try the lower case version  }else{    next(); // url is not special, and is already lower case  };});

Now keep in mind this middleware can be placed after all of your current routes, so that if it does not match an existing route you can try to look up what it should be. If you are using case insensitive route matching you would want to do this before your routes.


Using the same code as @user8377060just with a regex instead.

  // an array of all my urls  var urls = [    '/main',    '/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){         var currentUrl = new RegExp(item, 'i');         if(req.url != item && currentUrl.test(item)){           res.redirect(item);           console.log("redirected");           //redirected         }else if (req.url == item) {           console.log("correct url");           next();         }else{           //url doesn't exist         }     }    }    next();  });