Turn off the X-Powered-By setting to avoid advertising Express in responses.
Security hardening
Express.js · Module
app.disable(name) turns a documented Boolean Express application setting off by assigning it false. It returns the application instance for continued configuration.
Call app.disable(name) with the name of a Boolean application setting to set that setting to false. The method changes the application configuration and returns the application object, so it can participate in chained setup; setting a Boolean property to false with app.set() is equivalent.
app.disable(name)| Name | Type | Description |
|---|---|---|
name | String | Name of the Boolean application setting to disable; it must be one of the properties in the app settings table. |
Application
const express = require('express');
const app = express();
app.disable('trust proxy');
console.log(app.get('trust proxy')); // false
app.listen(3000);Creates an Express application, disables the Boolean trust-proxy setting, verifies the resulting false value, and starts a server.
Security hardening
Proxy configuration
Application setup
app.disable(name) sets the named Boolean application setting to false and returns the Application instance. For a Boolean property, app.set('foo', false) has the same effect.
Follow-up: How would you inspect the result? Use app.get(name), which returns the setting's current value; for a disabled Boolean setting it is false.
Pass the setting name as a string, such as 'trust proxy'. The name refers to a Boolean property in the application settings table.
Follow-up: What happens if the name is not a documented Boolean application setting? The page does not define behavior for that case, so it should not be relied on.
Disabling 'trust proxy' makes app.get('trust proxy') return false, as shown in the documentation example. This is application configuration rather than route middleware.
Follow-up: What equivalent configuration call is documented? app.set('trust proxy', false) expresses the same Boolean-setting change.
app.enable()app.enabled()app.disabled()app.set()app.get()