Saturday, March 26, 2022

Routing in Express

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Each Express router can have one or more handler functions, which are executed when the route is matched

© John Matychuk, CC-BY-SA 3.0

Route definition takes the following structure:

    app is an instance of Express.
    METHOD is an HTTP request method, in lowercase.
    PATH is a path on the server.
    HANDLER is the function executed when route is matched. Contributors

There are two different routing mechanisms in Express: the App Router, and Express Routers. The App Router lets you define routes at the global app level. This is useful for very simple applications with just a few routes. The Express Router on the other hand, provides a modular solution that allows you to define multiple routers, where each router handles a group of routes.

For example, you might have one router for user account related operations, another router for handling photos, etc.. And then you can mount each router at a specific path using app.use('/some/path', someRouter);

CSS Style for the Post:

Post a Comment

Note: Only a member of this blog may post a comment.