Express.js – app.enable() Method


The app.enable() function sets the Boolean setting ‘name’ to ‘true’, where name defines one of the properties from the app settings table. Using the app.set('foo', true) for a Boolean property is same as calling the app.enable('foo') function.

Syntax

app.enable(name)

Example 1

Create a file with the name "appEnable.js" and copy the following code snippet. After creating the file, use the command "node appEnable.js" to run this code.

// app.enable() Method Demo Example

// Importing the express module
const express = require('express');

// Initializing the express and port number
var app = express();

// Initializing the router from express
var router = express.Router();
var PORT = 3000;

app.enable('trust proxy')


console.log(app.get('trust proxy'));

// App listening on the below port
app.listen(PORT, function(err){
   if (err) console.log(err);
   console.log("Server listening on PORT", PORT);
});

Output

C:\home
ode>> node appEnable.js true Server listening on PORT 3000

Example 2

Let’s take a look at one more example.

// app.enable() Method Demo Example

// Importing the express module
const express = require('express');

// Initializing the express and port number
var app = express();

// Initializing the router from express
var router = express.Router();
var PORT = 3000;

app.enable('foo')

console.log("Is foo enabled --", app.get('foo'));

Output

C:\home
ode>> node appEnable.js Is foo enabled – true

Updated on: 30-Sep-2021

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements