Implement Routing
Learn about the implementation of routing in our project.
We'll cover the following...
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:
Press + to interact
const express = require("express");const app = express();// Route handler that sends a message to someoneapp.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 2: The
app
keyword refers to