Serve JSON (build an API)
Make your server speak the language of the web
⏱ Est. ~7 min
01 · Read
So far, your routes return plain text with res.send(). That's fine for simple messages, but real APIs don't return plain text — they return JSON.
Remember JSON from Level 5? It's the universal data format on the web. When your phone checks the weather, a site loads your profile, or an app fetches new posts — all that data is sent as JSON.
Express makes this super easy. Instead of res.send(), you use res.json() and pass a JavaScript object. Express automatically converts it to JSON and adds the right headers so browsers and apps know how to read it.
Key points
- res.send() returns plain text
- res.json() returns JSON data — the standard for APIs
- Express handles the JavaScript object to JSON conversion automatically
- This is how every API you've used works behind the scenes
02 · Real-machine exercise
Add a new JSON route to your app.js. This route returns the current time and date as structured data. Put it with your other routes (before app.listen()).
app.get('/api/time', (req, res) => {
res.json({
time: new Date().toLocaleTimeString(),
date: new Date().toLocaleDateString()
});
});03 · Quiz
What's the difference between res.send() and res.json()?
- res.send() is faster than res.json()
- res.json() only works with arrays
- No difference
- res.send() returns plain text, res.json() returns structured JSON data
04 · Real-machine exercise
Stop the server with Ctrl+C, restart it, then visit the new route in your browser. You should see JSON data with the current time and date — not plain text, but structured data with braces and key-value pairs.
node app.js
http://localhost:3000/api/time
05 · Fill in the blank
To return JSON data in Express, use res._____() instead of res.send().
06 · Read
Your server now speaks JSON — the same language as every API on the web.
Refresh the page a few times. Notice the time changes with every request? That's because your server re-runs the code each time someone visits the URL. It's dynamic — it generates a response based on the current moment.
This is the difference between a static website (same content every time) and an API (content that changes based on data, time, user, or anything else).
Remember learning about APIs and JSON in Level 5? You're now on the other side of that conversation. You're not just using APIs — you're making them.
07 · Quiz
Why does the /api/time endpoint return a different time every time you refresh?
- The server re-runs new Date() on every request, so it always returns the current time
- The browser is caching different versions of the page
- Express generates random timestamps for every response
- The JSON format automatically updates timestamps
Other lessons in this chapter
⚠ The full interactive experience needs JavaScript. Please enable it and reload.
※ This is an independent Traditional Chinese teaching project — not an official Anthropic product. Claude™ is a trademark of Anthropic, PBC.