Router

PussinSocks·2022년 7월 18일
0

CloneCoding

목록 보기
7/20

What is router?

Allows you to manage URLs and controllers. Known as "mini application". Router

  • Router let you minimize and organize the routes.
    for example, without routger, routes for the app would look like
/ => main page
/join => to make account
/login => to login
/search => to search items

/user-edit => to edit user info
/user-delete => to remove user info

/watch-vid => to watch video
/edit-vid => to edit video
/delete-video => to delete video
  • but if you use the router
/user/edit
/user/delete

/videos/watch
/videos/edit
/videos/delete
/videos/comments
/videos/comments/delete

routers let you categorize the routes by subjects you working on. (routes close to the root is called global routers)
and this makes easier to understand for users and developers.

How to make routers

const videoRouter = express.Router(); // 1.

const handleWatchVideo = (req, res) => res.send("Video Streaming Page"); // .4

routerName.get("/watch", handleWatchVideo); // 3. 

app.use("/videos", videoRouter); // 2.

When a user request for "/videos", instead of going to controller, it will go videoRouter, and look for the routes to get, which is "/watch" in here, and the handleWatchVideo controller will be executed. and forms /videos/watch address.

profile
On a journey to be a front-end developer

0개의 댓글