Express app.disable() Method
What you’ll learn
- How
app.disable(name)changes Express app-level settings. - How to disable
x-powered-byand other boolean settings correctly. - How to check setting state with
app.enabled()andapp.disabled(). - When to use
app.disable()vsapp.set()in real projects.
Overview
Use app.disable() to turn off boolean Express settings at application level.
Short and explicit
app.disable(name) is clear intent for turning a flag off.
Security-focused use
Most common example: disable x-powered-by in production.
Works with settings API
Pair with app.enable(), app.set(), and app.get() for full control.
Syntax
javascript
app.disable(name)- name: setting key to disable, e.g.
x-powered-by. - Equivalent to
app.set(name, false). - Check with
app.disabled(name)orapp.enabled(name).
1
Basic usage
javascript
const express = require('express');
const app = express();
app.disable('x-powered-by');
console.log(app.disabled('x-powered-by')); // true2
Production-safe setup
A minimal production baseline with explicit setting toggles.
javascript
const express = require('express');
const app = express();
if (process.env.NODE_ENV === 'production') {
app.disable('x-powered-by');
app.disable('etag');
}
app.get('/', function (req, res) {
res.send('Hello from Express');
});📋 app.disable() vs app.set()
| Method | Purpose | Example |
|---|---|---|
app.disable(name) | Set boolean setting to false | app.disable('x-powered-by') |
app.set(name, value) | Set setting to any value | app.set('trust proxy', 1) |
🧪 Testing checklist
- Disable a setting and verify with
app.disabled(name). - Send a response and inspect headers to confirm behavior (for example no
X-Powered-Byheader). - Run in development and production mode to verify conditional setup logic.
- Ensure other app settings still work as expected after toggling flags.
Pitfalls to avoid
Wrong setting key
No visible effect
Use exact setting names; typos silently disable the wrong key.
Environment mismatch
Unexpected behavior
Verify NODE_ENV when conditionally disabling settings.
Assuming request-level effect
Confusion in debugging
app.disable() changes app config, not route logic directly.
❓ FAQ
It sets a named Express application setting to false.
Yes. app.disable(name) is shorthand for app.set(name, false).
Use it in production to avoid exposing that your app uses Express via response headers.
Yes. Use app.enable(name) later to set the same flag to true again.
Not directly. It changes framework behavior tied to specific app settings.
Summary
- Purpose:
app.disable()turns a boolean app setting off. - Equivalent: it is shorthand for
app.set(name, false). - Common practice: disable
x-powered-byin production for cleaner headers.
Did you know?
app.disable(name) is equivalent to app.set(name, false) for Express application settings.
4 people found this page helpful
