Express.js res.links() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.links() populates the response Link HTTP header. You pass an object whose keys are relation names (such as next and last) and whose values are URL strings—or arrays of URLs for the same relation.

It does not send a body. Call it before res.json() or res.send(). For a single Location redirect target, use res.location() or res.redirect() instead.

Syntax

Call res.links in your Express.js app. Start with this line.

Express
res.links(links)
res.links({
next: 'http://api.example.com/users?page=2',
last: 'http://api.example.com/users?page=5'
});
res.links({
alternate: [
'https://example.com/feed.atom',
'https://example.com/feed.json'
]
});
CallWhat it does
res.links(links)Primary signature.
res.links({Related call form.
next: 'http://api.example.com/users?page=2',Related call form.
last: 'http://api.example.com/users?page=5'Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.links(links)
res.links({
next: 'http://api.example.com/users?page=2',
last: 'http://api.example.com/users?page=5'
});
res.links({
alternate: [
'https://example.com/feed.atom',
'https://example.com/feed.json'
]
});

Parameters

ParameterDescription
linksRequired. Object of relation → URL(s). Keys become rel values; each value is one URL or an array of URLs.
next / prevRequired. Next or previous page in a list
first / lastRequired. First or last page
selfRequired. Canonical URL of this resource
alternateRequired. Alternate representation (format, language, …)

Return Value

res.links() returns res for chaining after setting the Link header. It does not end the response.

Express
res.links({
  next: 'http://api.example.com/users?page=2',
  last: 'http://api.example.com/users?page=5'
}).json(users);

Express.js Examples

Example 1 — Official next + last Shape

Match the Express docs: set pagination links, then send JSON.

Express
const express = require('express');
const app = express();

app.get('/users', (req, res) => {
  res.links({
    next: 'http://api.example.com/users?page=2',
    last: 'http://api.example.com/users?page=5'
  });

  res.json({ page: 1, items: [{ id: 1, name: 'Mari' }] });
});

app.listen(3000);

How It Works

  1. Object keys are rel names. next and last become rel="next" and rel="last".
  2. Values are the URLs wrapped as <url> in the header.
  3. links does not finish the response — res.json sends the body.
  4. Clients read Link for navigation without guessing page math from the body alone.

Example 2 — Conditional Pagination Links

Only include relations that make sense for the current page.

Express
const express = require('express');
const app = express();

const TOTAL_PAGES = 5;

app.get('/posts', (req, res) => {
  const page = Math.max(1, Number(req.query.page) || 1);
  const base = 'http://api.example.com/posts';
  const links = {
    first: `${base}?page=1`,
    last: `${base}?page=${TOTAL_PAGES}`,
    self: `${base}?page=${page}`
  };

  if (page > 1) links.prev = `${base}?page=${page - 1}`;
  if (page < TOTAL_PAGES) links.next = `${base}?page=${page + 1}`;

  res.links(links);
  res.json({ page, totalPages: TOTAL_PAGES, items: [] });
});

app.listen(3000);

How It Works

  1. Build a links object with the relations you always want.
  2. Add prev/next only when valid so clients are not pointed past the ends.
  3. Call res.links once with that object, then send JSON.
  4. self helps clients know the canonical URL for this page.

Example 3 — Multiple URLs for One Relation

Pass a string array when one rel has several targets (docs: String | String[]).

Express
const express = require('express');
const app = express();

app.get('/article', (req, res) => {
  res.links({
    alternate: [
      'https://example.com/article.atom',
      'https://example.com/article.json'
    ],
    related: 'https://example.com/series/intro'
  });

  res.json({ title: 'Express Link headers', body: '…' });
});

app.listen(3000);

How It Works

  1. Array value → multiple Link segments with the same rel.
  2. String value → a single segment for that relation.
  3. Mix both in one res.links() call.
  4. Still send the body afterward with json or send.

Runtime Support

res.links() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Clients read the Link header from the HTTP response.

✓ Server · Express 4 & 5

res.links()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not a browser API.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.links() Express on Node.js

Bottom line: Study with Node.js and express. Use curl -i or DevTools Network to inspect the Link header.

Frequently Asked Questions

It joins an object of link relations (such as next and last) into the response Link HTTP header. Each property name becomes a rel value; each value is a URL string or an array of URLs.
Clients use it to discover related resources—especially pagination (next, prev, first, last)—without inventing a custom body shape.
No. It only sets the Link header. You still call res.json(), res.send(), or similar to finish the response.
Yes. Official type is Record<String, String | String[]>. Pass an array to emit multiple links for the same rel.
No. It is a stable Express 4 and Express 5 response method.

Next: res.locals

Pass request-scoped variables to templates and downstream handlers.

res.locals tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful