0
0
Expressframework~15 mins

req.query for query strings in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Using req.query to Read Query Strings in Express
📖 Scenario: You are building a simple Express server that responds to requests with personalized messages based on query strings in the URL.
🎯 Goal: Learn how to use req.query in Express to read query string parameters and use them in your response.
📋 What You'll Learn
Create an Express app with a GET route at /greet
Use req.query to read the name parameter from the URL
Send a greeting message that includes the name value
Add a default greeting if name is not provided
💡 Why This Matters
🌍 Real World
Web servers often use query strings to get information from users without needing forms. For example, search pages or filters use query strings.
💼 Career
Understanding how to read query strings with req.query is essential for backend developers working with Express to build APIs and web applications.
Progress0 / 4 steps
1
Set up Express app and route
Create an Express app by requiring express and calling express(). Then create a GET route at /greet with a callback function that takes req and res as parameters.
Express
Need a hint?

Remember to require Express and create an app instance before defining routes.

2
Read the name query parameter
Inside the /greet route callback, create a variable called name and set it to req.query.name to read the name query string parameter.
Express
Need a hint?

Use req.query to access query string parameters by name.

3
Send a greeting message using the name parameter
Still inside the /greet route, use res.send() to send a message that says Hello, {name}! if name exists. If name is missing, send Hello, guest! instead.
Express
Need a hint?

Use an if statement to check if name exists and send the appropriate message.

4
Start the server on port 3000
Add app.listen(3000) to start the Express server on port 3000.
Express
Need a hint?

Use app.listen(3000) to start the server on port 3000.