Express.js app.query() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, app.query() registers handlers for the HTTP QUERY method on a path. QUERY is for searches that need a body — filters that are too large, too nested, or too sensitive for a URL query string.

Unlike app.get(), QUERY carries input in the body. Unlike app.post(), QUERY is safe and idempotent. Mount express.json() first so req.body is filled.

Syntax

Call app.query in your Express.js app. Start with this line.

Express
app.query(path, callback)
app.query(path, ...callbacks)
CallWhat it does
app.query(path, callback)Register a QUERY route for one path.
app.query(path, ...callbacks)Same path with middleware then the handler.

Install with npm install express. Create the app with const app = express(). QUERY needs a modern Node.js (see Runtime support).

Parameters

ParameterDescription
pathRequired. Route path to match. Strings match the whole path.
callbackRequired. One or more handlers (req, res, next).

Return Value

app.query() returns the same application object (app), so chaining works. Most apps ignore the return value and write one route per line.

Express
app.query('/search', searchHandler);
// return value is app — usually unused

Express.js Examples

Example — Basic QUERY Route

Accept a JSON body on QUERY /search and echo it.

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

app.use(express.json());

app.query('/search', (req, res) => {
  res.json({ ok: true, filter: req.body });
});

app.listen(3000);

How It Works

  1. Parse JSON. express.json() fills req.body.
  2. Match QUERY. Only the QUERY verb hits this route.
  3. Read the body. Complex filters live in req.body, not the URL.
  4. Respond. res.json(...) returns the echo payload.

Example — When QUERY Beats a Long GET URL

Keep sensitive or huge filters out of the URL and server logs.

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

app.use(express.json());

app.query('/reports', (req, res) => {
  const customerId = req.body && req.body.customerId;
  const range = req.body && req.body.range;
  res.json({ customerId: customerId, range: range, rows: [] });
});

app.listen(3000);

How It Works

  1. Body holds criteria. Nested objects fit naturally in JSON.
  2. URL stays short. No giant query string or leaked secrets in history.
  3. Still a query. QUERY is safe/idempotent — not a create like POST.
  4. Parse first. Without express.json(), req.body is undefined.

Example — Validate Then Search

Reject empty bodies with a clear 400.

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

app.use(express.json());

app.query('/products', (req, res) => {
  if (!req.body || !req.body.term) {
    return res.status(400).json({ error: 'term is required' });
  }
  res.json({ term: req.body.term, hits: [] });
});

app.listen(3000);

How It Works

  1. Check the body. Missing term returns 400.
  2. Fail early. No database call on bad input.
  3. Happy path. Valid body continues to the search result.
  4. Same pattern as POST. Validation style matches other body routes.

Runtime Support

app.query() is an Express.js application method. The QUERY HTTP verb needs a recent Node.js. Browsers rarely send QUERY yet.

✓ Server · Express 5 · Node QUERY support

app.query()

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

Server Node.js runtime API
Node.js >=20.19.3 <21 or >=22.2.0 for QUERY
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.query() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It registers handlers for the HTTP QUERY method on a path. QUERY carries search input in the request body instead of the URL.
app.get() uses URL query strings. app.query() uses a body, which is better for large, nested, or sensitive filters.
Mount express.json() or another body parser before the route. Without a parser, Express does not fill req.body.
Support is still limited in clients and proxies. Confirm your Node.js version and target environments before production use.
No. Express runs on a Node.js server. Clients must send an HTTP QUERY request to your API.

Next: app.render()

Render a view to an HTML string outside a normal request cycle.

app.render() 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