Express.js res.jsonp() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.jsonp() sends a JSON response with JSONP support. Official docs: it is identical to res.json(), except it opts in to wrapping the payload in a JavaScript callback when the client passes a callback query parameter.

By default the query key is callback (?callback=foo → foo({…})). Override with app.set('jsonp callback name', 'cb'). For new APIs, prefer res.json() plus CORS instead of JSONP.

Syntax

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

Express
res.jsonp([body])
res.jsonp(null);
res.jsonp({ user: 'tobi' });
res.status(500).jsonp({ error: 'message' });
app.set('jsonp callback name', 'cb');
CallWhat it does
res.jsonp([body])Primary signature.
res.jsonp(null);Related call form.
res.jsonp({ user: 'tobi' });Related call form.
res.status(500).jsonp({ error: 'message' });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.jsonp([body])
res.jsonp(null);
res.jsonp({ user: 'tobi' });
res.status(500).jsonp({ error: 'message' });
app.set('jsonp callback name', 'cb');

Parameters

ParameterDescription
bodyRequired. Value to send. Same JSON types as res.json() (object, array, string, Boolean, number, null, …).
jsonp callback nameRequired. Query string key that supplies the wrapper function name

Return Value

res.jsonp() sends JSON (with optional JSONP wrapping) and returns res. Like res.json, it finishes the response.

Express
res.jsonp({ user: 'tobi' });
// with ?callback=foo → foo({"user":"tobi"})

Express.js Examples

Example 1 — JSONP for a Legacy Client

Send user data; wrap when ?callback= is present.

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

app.get('/api/legacy-user', (req, res) => {
  res.jsonp({ id: 1, name: 'Mari' });
});

// Docs-style equivalents:
// res.jsonp(null);           // => callback(null)
// res.jsonp({ user: 'tobi' }); // => callback({ "user": "tobi" })

app.listen(3000);

How It Works

  1. Same payload as json. You pass a normal object.
  2. Callback query opts in. Default key is callback.
  3. Body becomes a call. ?callback=foo → foo({…}) per the docs.
  4. Legacy script tags can load that URL and run the function.

Example 2 — Customize the Callback Query Key

Use app.set('jsonp callback name', 'cb') when clients send ?cb=.

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

app.set('jsonp callback name', 'cb');

app.get('/api/legacy-stats', (req, res) => {
  res.jsonp({ ok: true, visits: 1200 });
});

app.listen(3000);

How It Works

  1. Setting renames the query key. Official: override with jsonp callback name.
  2. Value of that key is still the function name (foo).
  3. jsonp wraps with that name. Same as docs: ?cb=foo → foo(…).
  4. Set once at startup so every JSONP route shares the name.

Example 3 — Status Code with JSONP

Chain status like res.json — including the docs’ 500 error shape.

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

app.get('/api/legacy-orders/:id', (req, res) => {
  if (!/^\d+$/.test(req.params.id)) {
    return res.status(400).jsonp({ error: 'Invalid id' });
  }
  res.jsonp({ id: Number(req.params.id), status: 'queued' });
});

// Docs:
// res.status(500).jsonp({ error: 'message' });
// ?callback=foo → foo({ "error": "message" })

app.listen(3000);

How It Works

  1. status then jsonp. Same chaining style as res.status().json().
  2. return on errors so you do not send twice.
  3. Callback still wraps the error object when the query is present.
  4. HTTP status remains meaningful for XHR clients; script-tag clients mainly execute the function body.

Runtime Support

res.jsonp() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Clients receive either JSON or a callback-wrapped script body.

✓ Server · Express 4 & 5

res.jsonp()

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.jsonp() Express on Node.js

Bottom line: Study with Node.js and express. For new APIs, prefer res.json() and CORS; use jsonp only for legacy needs.

Frequently Asked Questions

It is identical to res.json(), except it opts in to JSONP callback support. When a callback query param is present, the JSON is wrapped like callback({ ... }).
callback. So ?callback=foo wraps the payload as foo(...). Change it with app.set('jsonp callback name', 'cb').
Mainly for legacy clients that load data via a script tag. Modern APIs should use res.json() with CORS instead.
Yes. Example from the docs: res.status(500).jsonp({ error: 'message' }).
No — the method is still available. It is a legacy cross-domain pattern; prefer CORS + JSON for new work.

Next: res.links()

Populate the Link header from a map of relations.

res.links() 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