URL parameters

PussinSocks·2022년 7월 19일
0

CloneCoding

목록 보기
9/20

parameters

router.get(":parameter", controller);
It is like ${variableName} inside of HTML elements.
It lets you put variables inside of the routes.

You can check the request parameters.

export const see = (req, res) => {
    console.log(req.params);
    res.send("See Video");
}

When you go to the URL, console shows
/video/2349 => { id: '2349' }
/video/see => { id: 'see' }


Problem. Because we didn't set any rules on the parameters, any number and letters can be inserted into the parameter. How should we regulate this?

Regular Expression (Regex)

  • Since we want the :id get only numbers, we use \d+
videoRouter.get("/:id(\\d+)", see);
videoRouter.get("/:id(\\d+)/edit", edit);
videoRouter.get("/:id(\\d+)/delete", deleteVideo);
videoRouter.get("/upload", upload);
  • Now order of the route is not important anymore.

You can practise Regex here: regexpal

profile
On a journey to be a front-end developer

0개의 댓글