0
0
Expressframework~30 mins

Why understanding res matters in Express - See It in Action

Choose your learning style9 modes available
Why understanding res matters
📖 Scenario: You are building a simple Express server that sends responses to users. Understanding how the res object works is important to send the right messages back.
🎯 Goal: Build a basic Express server that uses the res object to send a text message and a JSON response to the client.
📋 What You'll Learn
Create an Express app variable called app
Create a route handler for GET requests on /hello
Use res.send() to send a plain text message
Create a route handler for GET requests on /data
Use res.json() to send a JSON object
💡 Why This Matters
🌍 Real World
Express servers are used to build web applications and APIs that send data and pages to users. Knowing how to use <code>res</code> helps you control what users see.
💼 Career
Backend developers must understand how to send correct responses using <code>res</code> in Express to build reliable and user-friendly web services.
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 to create the app.

2
Add a simple text response route
Add a GET route handler on /hello using app.get. The handler function should take req and res as parameters. Use res.send() to send the text message 'Hello, world!'.
Express
Need a hint?

Use app.get('/hello', (req, res) => { res.send('Hello, world!'); }) to send a text response.

3
Add a JSON response route
Add a GET route handler on /data using app.get. The handler function should take req and res as parameters. Use res.json() to send the JSON object { message: 'This is JSON data' }.
Express
Need a hint?

Use res.json({ message: 'This is JSON data' }) to send JSON data.

4
Start the Express server
Add code to start the server by calling app.listen on port 3000. Use a callback function that logs 'Server running on port 3000'.
Express
Need a hint?

Use app.listen(3000, () => { console.log('Server running on port 3000'); }) to start the server.