0
0
Expressframework~30 mins

Why routing matters in Express - See It in Action

Choose your learning style9 modes available
Why routing matters
📖 Scenario: You are building a simple web server that responds differently based on the URL path the user visits. This is like a restaurant where different doors lead to different rooms. Routing helps the server know which response to send for each door (URL).
🎯 Goal: Create an Express server with routes for the homepage, about page, and contact page. Each route should send a unique message to the browser.
📋 What You'll Learn
Create an Express app instance
Set up a route for '/' that sends 'Welcome to the homepage!'
Set up a route for '/about' that sends 'Learn more about us.'
Set up a route for '/contact' that sends 'Contact us here.'
Start the server listening on port 3000
💡 Why This Matters
🌍 Real World
Routing is how web servers decide what content to show when users visit different URLs. This is essential for websites and APIs.
💼 Career
Understanding routing is a key skill for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Need a hint?

Use require('express') to import Express and then call it as a function to create the app.

2
Add homepage route
Use app.get with the path '/' and a callback function with parameters req and res. Inside the callback, send the text 'Welcome to the homepage!' using res.send().
Express
Need a hint?

Use app.get to define a route for the homepage path '/' and send a welcome message.

3
Add about and contact routes
Add two more routes using app.get: one for '/about' that sends 'Learn more about us.', and one for '/contact' that sends 'Contact us here.'. Use req and res as parameters in the callback functions.
Express
Need a hint?

Define two more routes with app.get for '/about' and '/contact' paths, sending the specified messages.

4
Start the server
Use app.listen to start the server on port 3000. Add a callback function that takes no parameters and does nothing inside.
Express
Need a hint?

Use app.listen with port 3000 and an empty callback function to start the server.