Pagination navigation
A collection endpoint can advertise the URL for the next page and the final page through Link relations.
Express.js · Module
res.links() populates the HTTP Link response header from relation names mapped to URLs. It is useful for communicating pagination or other related-resource links.
Call res.links with an object whose property names are link relations and whose values are their URLs. Express converts those entries into a Link response header, then returns the response object so response methods can be chained or followed by sending the body.
res.links(links)| Name | Type | Description |
|---|---|---|
links | Record<String, String> | An object whose properties, such as next and last, populate the Link response header. |
Response
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
res.links({
next: 'https://api.example.com/users?page=2',
last: 'https://api.example.com/users?page=5'
});
res.json([{ id: 1, name: 'Ada' }]);
});
app.listen(3000);Adds navigation URLs to the response's Link header before sending JSON.
A collection endpoint can advertise the URL for the next page and the final page through Link relations.
An API can expose URLs for related resources without embedding every related object in the response body.
A response can describe alternate or neighboring representations using named link relations.
res.links(links) accepts a Record<String, String> and returns the Response object. It joins each supplied relation-to-URL property into the HTTP Link response header.
Follow-up: What header does a pagination example produce? It contains angle-bracketed URLs followed by rel attributes, such as <URL>; rel="next".
Pass an object such as { next: '...page=2', last: '...page=5' }. Express serializes those properties into one Link header, which describes related resources or navigation targets.
Follow-up: How would you use it in a route? Call res.links before res.json or another response-ending method so the header is set before the response is sent.
The documented 4.x method accepts a single links argument typed as Record<String, String>. The method returns Response, allowing continued use of the response object.
Follow-up: What changed in newer documentation? Later API documentation also permits each relation value to be an array of URL strings, but that is not the 4.x signature used here.
res.set()res.header()res.json()res.send()