HATEOAS helps your app tell clients what they can do next by giving links in responses. It makes APIs easier to use and explore without extra instructions.
HATEOAS concept overview in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
res.json({
data: {...},
links: [
{ rel: 'self', href: '/resource/1' },
{ rel: 'update', href: '/resource/1' },
{ rel: 'delete', href: '/resource/1' }
]
});The response includes a links array with objects describing possible next actions.
Each link has a rel (relation) describing the action and an href URL to perform it.
Examples
Express
res.json({
data: { id: 1, name: 'Book' },
links: [
{ rel: 'self', href: '/books/1' },
{ rel: 'author', href: '/authors/5' }
]
});Express
res.json({
data: { id: 2, status: 'pending' },
links: [
{ rel: 'approve', href: '/orders/2/approve' },
{ rel: 'cancel', href: '/orders/2/cancel' }
]
});Sample Program
This Express server sends book data with HATEOAS links showing what actions the client can take next.
Express
import express from 'express'; const app = express(); app.get('/books/:id', (req, res) => { const bookId = req.params.id; // Simulated book data const book = { id: bookId, title: 'Learn Express' }; res.json({ data: book, links: [ { rel: 'self', href: `/books/${bookId}` }, { rel: 'update', href: `/books/${bookId}` }, { rel: 'delete', href: `/books/${bookId}` } ] }); }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));
Important Notes
HATEOAS responses help clients navigate your API without guessing URLs.
Keep links relevant to the current resource and user permissions.
Use clear rel names so clients understand the actions easily.
Summary
HATEOAS adds links in API responses to guide clients on next steps.
It makes APIs easier to use and discover without extra documentation.
Express apps can send HATEOAS links as part of JSON responses.
Practice
1. What is the main purpose of HATEOAS in an Express API?
easy
Solution
Step 1: Understand HATEOAS concept
HATEOAS stands for Hypermedia As The Engine Of Application State, which means APIs provide links to guide clients on what to do next.Step 2: Identify main purpose in Express API
Express apps use HATEOAS by sending links in JSON responses to help clients discover available actions without extra docs.Final Answer:
To include links in responses that guide clients on possible next actions -> Option CQuick Check:
HATEOAS guides clients with links = C [OK]
Hint: HATEOAS means adding helpful links in API responses [OK]
Common Mistakes:
- Thinking HATEOAS speeds up server
- Confusing HATEOAS with encryption
- Believing it reduces JSON size
2. Which of the following is the correct way to include a HATEOAS link in an Express JSON response?
easy
Solution
Step 1: Identify JSON response with HATEOAS links
HATEOAS links are included as part of JSON, usually in a 'links' array with 'rel' and 'href' keys.Step 2: Check Express syntax for sending JSON
res.json() sends JSON data; res.json({ data: user, links: [{ rel: 'self', href: '/users/1' }] }); correctly uses 'links' array with proper structure.Final Answer:
res.json({ data: user, links: [{ rel: 'self', href: '/users/1' }] }); -> Option AQuick Check:
HATEOAS links in JSON with rel/href = A [OK]
Hint: HATEOAS links go inside JSON under 'links' key [OK]
Common Mistakes:
- Sending HTML instead of JSON
- Using 'url' instead of 'links' array
- Rendering views instead of JSON
3. Given this Express route code, what will the JSON response include?
app.get('/books/:id', (req, res) => {
const book = { id: req.params.id, title: 'Learn Express' };
res.json({
data: book,
links: [
{ rel: 'self', href: `/books/${book.id}` },
{ rel: 'author', href: `/authors/123` }
]
});
});medium
Solution
Step 1: Analyze the route handler
The route sends JSON with 'data' containing book info and 'links' array with two link objects.Step 2: Confirm template literals usage
Template literals are valid in modern JavaScript, so href values will be correct URLs.Final Answer:
JSON with book data and two links: self and author -> Option DQuick Check:
Response includes data and links array = A [OK]
Hint: Look for 'links' array in JSON response to find HATEOAS links [OK]
Common Mistakes:
- Assuming no links are sent
- Confusing JSON with HTML output
- Thinking template literals cause errors
4. What is wrong with this Express code snippet trying to implement HATEOAS?
app.get('/items/:id', (req, res) => {
const item = { id: req.params.id, name: 'Item A' };
res.json({
data: item,
links: {
rel: 'self',
href: `/items/${item.id}`
}
});
});medium
Solution
Step 1: Check 'links' structure for HATEOAS
HATEOAS expects 'links' to be an array of link objects, not a single object.Step 2: Validate other code parts
Template literals are valid, res.json is correct, and status code defaults to 200, so no issues there.Final Answer:
The 'links' property should be an array, not an object -> Option BQuick Check:
'links' must be array for multiple links = D [OK]
Hint: 'links' must be an array of objects, not a single object [OK]
Common Mistakes:
- Using object instead of array for 'links'
- Thinking template literals are invalid
- Replacing res.json with res.send unnecessarily
5. You want to design an Express API that uses HATEOAS to help clients navigate a blog. Which approach best applies HATEOAS principles?
hard
Solution
Step 1: Recall HATEOAS goal
HATEOAS guides clients by embedding links in responses to related resources or actions.Step 2: Evaluate options for blog API
Include in each blog post response links to 'self', 'author', and 'comments' endpoints includes links to related endpoints in each response, matching HATEOAS principles. Options A, C, and D do not embed navigational links in responses.Final Answer:
Include in each blog post response links to 'self', 'author', and 'comments' endpoints -> Option AQuick Check:
Embed navigational links in response = B [OK]
Hint: Embed related resource links inside each response for HATEOAS [OK]
Common Mistakes:
- Skipping links to keep response small
- Relying only on external docs
- Using query params instead of links
