Express.js req.app Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

req.app holds a reference to the Express application that is handling the request — the same object you create with express().

It shines when a middleware file only exports a handler and cannot close over the app variable. Inside that handler, call req.app.get('views') (or any setting) to reach the app. It is not a browser API; it exists on the server-side request object.

Syntax

Read req.app in your Express.js route handler. Start with this line.

Express
req.app
req.app.get(name)
req.app.set(name, value)
req.app.locals
CallWhat it does
req.appPrimary signature.
req.app.get(name)Related call form.
req.app.set(name, value)Related call form.
req.app.localsRelated 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();

req.app
req.app.get(name)
req.app.set(name, value)
req.app.locals

Parameters

ParameterDescription
Express app instanceRequired. Created once; req.app points at it during the request
Middleware / route handlerRequired. Where you read req.app
Settings via app.setOptional. Config that handlers later read with req.app.get

Return Value

req.app is a property that refers to the Express application handling this request. Reading it returns that app instance — useful for settings and shared app state.

Express
const title = req.app.get('title');
const env = req.app.get('env');

Express.js Examples

Example 1 — Access the App from a Middleware Module

Export a handler that never imports app. It still reaches settings through req.app (the official docs pattern).

Express
// mymiddleware.js
module.exports = (req, res) => {
  res.send('The views directory is ' + req.app.get('views'));
};
Express
const express = require('express');
const app = express();

app.get('/viewdirectory', require('./mymiddleware'));

app.listen(3000);

How It Works

  1. Keep the module focused. mymiddleware.js only exports a function — no require of the main app.
  2. Wire it in the main file. app.get('/viewdirectory', require('./mymiddleware')) attaches that handler.
  3. Reach the app via the request. Inside the handler, req.app is the same Express instance.
  4. Read a built-in setting. req.app.get('views') returns the views path Express uses for templates.

Example 2 — Read a Custom Setting

Store config with app.set, then read it from any route through req.app.get.

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

app.set('siteName', 'CodeToFun');
app.set('supportEmail', '[email protected]');

app.get('/info', (req, res) => {
  res.json({
    site: req.app.get('siteName'),
    support: req.app.get('supportEmail'),
    sameAsRes: req.app === res.app
  });
});

app.listen(3000);

How It Works

  1. Set config once. app.set('siteName', …) stores values on the application.
  2. Read from the request. Any handler can call req.app.get('siteName') without a global import.
  3. Confirm res.app matches. req.app === res.app is true for the same request.
  4. Return JSON. Useful for health or about endpoints that expose non-secret config.

Example 3 — req.app Inside a Mounted Sub-App

When you mount a second express() app, handlers in that sub-app see the sub-app as req.app.

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

app.set('siteName', 'Main App');
admin.set('siteName', 'Admin App');

admin.get('/', (req, res) => {
  res.json({
    site: req.app.get('siteName'),
    mountpath: req.app.mountpath
  });
});

app.use('/admin', admin);

app.listen(3000);

How It Works

  1. Create two apps. app is the parent; admin is a sub-app with its own settings.
  2. Mount the sub-app. app.use('/admin', admin) attaches it under /admin.
  3. req.app is the sub-app. Inside admin.get('/'), settings come from admin, not the parent.
  4. Check mountpath. req.app.mountpath is /admin — see also app.mountpath.

Runtime Support

req.app is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers never see this object.

✓ Server · Express 4 & 5

req.app

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 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.app Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Use req.app whenever a handler needs the application without a global import.

Frequently Asked Questions

It is a reference to the Express application instance that is currently handling the request — the same object you create with express().
Use it in middleware or route modules that do not close over the app variable, especially when a file only exports a handler and needs app settings.
Call req.app.get('settingName'), the same API as app.get for settings. Values come from app.set() or Express defaults like views.
Yes. res.app is identical to req.app — both point at the application using the middleware.
While a sub-app handles the request, req.app is that sub-app (not the parent). After the sub-app finishes, Express restores the parent prototypes.

Next: req.baseUrl

See the URL path on which a router instance was mounted.

req.baseUrl 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