How to use routing in Express

Route is an important component of a website. It helps the website know where the user is going to the website, thereby responding appropriately.

In Express, routes are built-in and easy to use. In this article, let’s learn about routes in Express.

I. Routing in Express

Routing in Node.js determines how the application will respond when a user makes a request to a particular endpoint.

That endpoint is usually a URI or a path (Path) with a specific request method (POST, PUT, GET, DELETE)

In express.js routing has the following structure

app.METHOD(Path, Handler...)
  • app : is an instance of express
  • METHOD: is an HTTP Method
  • Path: is a path on the server.
  • Handler: a callback function that will execute when a route is matched

Example

app.get("/hello", (req, res) => {
  res.send("Hello World");
});

II. Basic route handling

Within a route, we can basically do anything, such as

  • Fetch data from database with MongoDB, MySQL
  • Load pages
  • Return JSON data
  • Access requests and response

Express also has a router so we can store routes in seperate files and make our codes cleaner.

Note

  • Request object represents the HTTP request property, any data sent with the body such as URL parameters, query strings, HTTP headers.
  • Response object represent HTTP responses. We can use it to send back JSON data or do anything we want.

III. Route methods

Express supports many different types of HTTP methods, and GET, POST, PUT, DELETE are the most used.

// respond with "Hello World!" on the homepage
app.get("/", function (req, res) {
  res.send("Hello World!");
});

// accept POST request on the homepage
app.post("/", function (req, res) {
  res.send("Got a POST request");
});

// accept PUT request at /user
app.put("/user", function (req, res) {
  res.send("Got a PUT request at /user");
});

// accept DELETE request at /user
app.delete("/user", function (req, res) {
  res.send("Got a DELETE request at /user");
});

With the above declaration, when we access the address localhost:3000/user by GET, POST, PUT, DELETE methods, the server will send back a corresponding text.

When we access /hello, then the route will match and the function will be called.

IV. Route paths

Route paths can be strings, string patterns, or regular expressions.

  • /public/post: is a regular path
  • /public/user/*: is a path with the symbol * representing any string
  • /.*hello$/: is a path in the form of a regex.

Example 1: String pattern

The * symbol here means that the above route matches all paths starting with /users/.

app.get("/users/*", (req, res) => {
  // Do something.
});

Example 2:

This is a path with a regular expression. This route will match any path ending with hello.

app.get(/.*hello$/, (req, res) => {
  // Do something.
});

V. Routing Parameters

Route parameters are named URL segments that can retrieve the corresponding values.

// Get user's info by id
app.get("/user/:id", (req, res) => {
  let id = req.params["id"];
  res.send("The id is " + id);
});

With the above declaration, when we access the address localhost:3000/user/125, the server will return you the text: The id is 125.