0
0
Expressframework~15 mins

res.redirect for redirections in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Using res.redirect for Redirections in Express
📖 Scenario: You are building a simple Express web server that needs to send users to different pages based on certain conditions.
🎯 Goal: Learn how to use res.redirect to send users to another URL in an Express route.
📋 What You'll Learn
Create an Express app with a route at /start
Create a variable to hold the URL to redirect to
Use res.redirect to send the user to the URL stored in the variable
Add a route at /welcome that sends a welcome message
💡 Why This Matters
🌍 Real World
Web servers often need to send users to different pages or sites. Using res.redirect helps guide users smoothly.
💼 Career
Understanding how to redirect users is essential for backend web developers working with Express or similar frameworks.
Progress0 / 4 steps
1
Set up Express app and create /start route
Write code to import Express, create an app, and add a route at /start that takes req and res as parameters.
Express
Need a hint?

Use require('express') to import Express and express() to create the app. Then use app.get to create the route.

2
Create a variable redirectUrl with value '/welcome'
Inside the /start route, create a variable called redirectUrl and set it to the string '/welcome'.
Express
Need a hint?

Use const redirectUrl = '/welcome'; inside the route function.

3
Use res.redirect to send user to redirectUrl
In the /start route, after creating redirectUrl, use res.redirect(redirectUrl) to redirect the user.
Express
Need a hint?

Call res.redirect with the variable redirectUrl as argument.

4
Add /welcome route that sends a welcome message
Add a new route at /welcome that sends the text 'Welcome to the site!' using res.send. Also add app.listen(3000) to start the server.
Express
Need a hint?

Use app.get to create the /welcome route and res.send to send the message. Then start the server with app.listen(3000).