0
0
Expressframework~30 mins

res.json for JSON responses in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Using res.json for JSON responses in Express
📖 Scenario: You are building a simple Express server that sends JSON data to clients. This is common when creating APIs that share data with web or mobile apps.
🎯 Goal: Create an Express server that responds with JSON data using res.json(). You will set up data, configure a route, send the JSON response, and complete the server setup.
📋 What You'll Learn
Create a data object with exact key-value pairs
Set up an Express app and a port variable
Create a GET route that sends the data as JSON using res.json()
Start the server listening on the specified port
💡 Why This Matters
🌍 Real World
APIs often send data as JSON to web or mobile apps. Using res.json() in Express is a simple way to send this data.
💼 Career
Backend developers use Express and res.json() to build APIs that communicate data efficiently and clearly.
Progress0 / 4 steps
1
DATA SETUP: Create the data object
Create a constant called userData with this exact object: { name: "Alice", age: 30, city: "Wonderland" }
Express
Need a hint?

Use const to create an object with the keys name, age, and city.

2
CONFIGURATION: Set up Express app and port
Write these two lines: const express = require('express'); and const app = express(); Then create a constant port set to 3000
Express
Need a hint?

Use require('express') to import Express, then create the app and port variables.

3
CORE LOGIC: Create GET route sending JSON response
Use app.get with path "/user" and a callback with parameters req and res. Inside, send userData as JSON using res.json(userData).
Express
Need a hint?

Use app.get with the path "/user" and send the userData object as JSON with res.json().

4
COMPLETION: Start the server listening on the port
Add app.listen(port, () => { console.log(`Server running on port ${port}`); }); to start the server.
Express
Need a hint?

Use app.listen with the port variable and a callback that logs the server start message.