Express app.set() Method
What you’ll learn
- How to set app-level configuration with
app.set(name, value). - How to retrieve values with
app.get(name). - How
app.set()relates toapp.enable()andapp.disable(). - How to avoid common setting and environment mistakes.
Overview
app.set() is the core configuration API for storing and updating Express application settings.
App configuration
Set built-in options like view engine and views.
Custom settings
Store reusable app-level values for use across modules.
Foundation API
app.enable() and app.disable() are convenience wrappers around app.set().
Syntax
javascript
app.set(name, value)- name: setting key (for example
view engine). - value: any value type (string, boolean, number, object).
- Read later with
app.get(name).
1
Set view engine and views path
javascript
const path = require('path');
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));2
Store and read custom config
javascript
app.set('supportEmail', 'support@codetofun.com');
app.set('featureFlagNewUI', true);
console.log(app.get('supportEmail'));
console.log(app.get('featureFlagNewUI'));📋 app.set() vs app.enable()/app.disable()
| Method | Purpose | Example |
|---|---|---|
app.set(name, value) | Set any value type | app.set('etag', 'strong') |
app.enable(name) | Set boolean true | app.enable('trust proxy') |
app.disable(name) | Set boolean false | app.disable('x-powered-by') |
🧪 Testing checklist
- Set values and verify them with
app.get(name). - Validate rendering setup after configuring
view engineandviews. - Test boolean settings using
app.enabled()andapp.disabled(). - Check environment-specific overrides behave as expected.
Pitfalls to avoid
Setting key typos
Unexpected behavior
Small key mismatches lead to hard-to-track config bugs.
Mixed value types
Inconsistent reads
Keep setting value types predictable across environments.
Late configuration
Startup surprises
Set critical config before middleware/routes that depend on it.
❓ FAQ
It stores a setting value at the application level, which can later be read with app.get(name).
Common examples include view engine, views directory path, and various Express behavior settings.
app.enable(name) is shorthand for app.set(name, true), and app.disable(name) is shorthand for app.set(name, false).
Yes. You can store custom keys and values to share app-level configuration.
Use app.get(name) to retrieve the current value for that setting key.
Summary
- Core use:
app.set()stores application configuration values. - Flexibility: supports built-in and custom setting keys.
- Practice: keep config explicit and verify values early at startup.
Did you know?
app.set(name, value) stores application-level configuration values such as view engine, views, or custom app settings.
4 people found this page helpful
