Search⌘ K
AI Features

Implement Routing

Explore how to implement routing in Express by creating route handlers for various HTTP methods and paths. Understand how to build dynamic routes with parameters to personalize server responses. This lesson equips you to handle client requests effectively in Node.js RESTful applications.

Our application is still missing the routing. Therefore, let’s learn about it and introduce routes in our project.

Routing

Routing refers to how an application handles a client request to a particular endpoint. An endpoint consists of a path, such as /hello in https://www.helloworld.com/hello, and an HTTP method, which could be GET, PUT, POST, PATCH, or DELETE.

Task 6: Express route handler

Inside src/index.js, create a route handler after instantiating the Express application:

Javascript (babel-node)
const express = require("express");
const app = express();
// Route handler that sends a message to someone
app.get("/", (req, res) => {
res.send("Hello Express Student!");
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is up on port ${port}.`);
});

Let’s break down the syntax here:

  • Line
...