0

Problem

Hi devs,

I am having trouble passing an id that has a '/' at the beginning.

This is the log

GET /api/v1/ GetAnimeInfo//anime/5226/tokyo-ghoul/Tokyo% 20Ghoul 404 0.466 ms - 1310

As you can see, He can not recognize two / after GetAnimeInfo//

Isn't there a way expressjs allows me that pattern?

//id = '/anime/5226/tokyo-ghoul/'
//title = 'Tokyo Ghoul'

router.get('/GetAnimeInfo/:id([^/]+/[^/]+[^/]+/[^/])/:title' , (req , res , next) =>{
  let id = req.params.id
  let title = req.query.title;
  api.getAnimeInfo(id , title)
    .then(info =>{
      res.status(200).json({
        info
      });
    }).catch((err) =>{
      console.log(err);
    });
});

Chris Michael
  • 1,557
  • 3
  • 15
  • 23

2 Answers2

2

I would highly advise against doing this.

If a client is sending an erroneous double slash there is a functional bug creating that issue and you should fix the bug, not provide a weird workaround on the server - that way you end up with more robust, predictable and maintainable code in the future.

If you're trying to manipulate the server to accept double slash as part of routing, there will be no guarantee that clients will respect the behavior so you will run into situations where one browser will work and another will not.

If you have shows which begin in a slash, eg '/ShowName', that you need to account for, you should be escaping the show name with URL encoding - https://en.wikipedia.org/wiki/Percent-encoding

testUser12
  • 171
  • 2
  • 9
  • I don't really believed thats the case, it looks more of a misunderstsnding of how to pass the data – James Feb 03 '20 at 07:25
  • It doesn't matter where the misunderstanding is, fixing it by modifying server-side routes to work around the issue isn't a good way to fix the problem. – testUser12 Feb 03 '20 at 10:09
  • again, I don't think the user intentionally wants to send a double backslash, I don't think they understand that this should be a single URL. Thats my interpretation of the question anyway. – James Feb 03 '20 at 10:31
0

Yeah, that's unlikely to work given Express would have no idea where where the :id ends and where the rest of the URL pattern match begins.

Can't you just parse the URL manually? Doesn't seem like it would be all that difficult e.g.

router.get('/GetAnimeInfo/:idAndTitle', (req, res, next) => {
  const { idAndTitle } = req.params;
  const idx = idAndTitle.lastIndexOf("/") + 1;
  const id = idAndTitle.substring(0, idx);
  const title = idAndTitle.substring(idx, idAndTitle.length);
  ...
});

Demo

const idAndTitle = '/anime/5226/tokyo-ghoul/Tokyo Ghoul';
const idx = idAndTitle.lastIndexOf("/") + 1;
const id = idAndTitle.substring(0, idx);
const title = idAndTitle.substring(idx, idAndTitle.length);
console.log(`ID=${id}`);
console.log(`Title=${title}`);
James
  • 80,725
  • 18
  • 167
  • 237
  • I really appreciate your solution, but I can't join both parameters. Since I have configured the getAnimeInfo function to take 2 parameters. – Chris Michael Feb 03 '20 at 01:06
  • @ChrisMichael you are only joining the parameters for the sake of the Express route, you parse them out separate and use however you like (see demo) – James Feb 03 '20 at 01:11