Open In App

Express.js app.locals Property

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Express.js, app.locals is an object used to store variables that need to be available throughout the app. These variables can be accessed globally across different routes or middleware and are often used for configuration settings or values that should be shared

What is app.locals?

app.locals is an object that holds local variables which are accessible throughout the entire Express application. These variables are typically used to store global settings such as application configurations, shared data, or values that need to be passed to views and templates. They are stored as key-value pairs and are available to all routes and middleware.

Syntax

app.locals
  • No parameters are needed to access the app.locals object.
  • It returns an object containing all the local variables for the application.

Installation of Express Module

Before using app.locals, you must first install the Express.js module. Follow these steps to set up your Express project

Step1: Install Express using npm

You can install Express by running the following command

npm install express

Step 2: Set up your project

After installing Express, create a project folder and add a file, such as index.js. To run this file, use the following command

node index.js

Example Usage of app.locals

Example 1: Simple Local Variables

This code sets up a basic Express.js server and demonstrates how to use app.locals to store and access global variables

JavaScript
//index.js
const express = require('express');
const app = express();

app.locals.email = '[email protected]'; // Setting a global variable

app.get('/', (req, res) => {
  res.send(app.locals.email); // Accessing the global variable
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Output

Screenshot-2025-03-17-143830
visit localhost:3000
  • Importing Express: The code starts by importing the Express module, which is a framework for building web applications in Node.js
  • Setting a Global Variable: app.locals.email = '[email protected]'; sets a global variable called email on the app.locals object, which can be accessed across all routes in the app
  • Creating a Route: The app.get() method defines a route for the root URL ('/'). When a user visits this URL, it sends the email stored in app.locals as the response
  • Starting the Server: app.listen(3000, () => { console.log('Server running on port 3000'); }); starts the server on port 3000 and logs a message when the server is running

Example 2: Storing Multiple Variables

This code sets up an Express web server and defines multiple global variables accessible throughout the app

JavaScript
//index.js
const express = require('express');
const app = express();

app.locals.domain = 'www.sample.com';  // Setting global domain
app.locals.age = '24';  // Setting global age
app.locals.company = 'ABC Ltd';  // Setting global company

app.get('/', (req, res) => {
  res.send(app.locals);  // Accessing multiple global variables
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Output

Screenshot-2025-03-17-144300
visit localhost:3000
  • Importing Express: The code imports the Express module and initializes an Express application
  • Setting Global Variables: app.locals is used to set global variables (domain, age, company) that are accessible throughout the app
  • Creating a Route: A route is defined for the root URL (/), which sends the entire app.locals object (containing all global variables) as the response when accessed
  • Starting the Server: The server listens on port 3000, and a message is logged when the server starts running

Benefits of Using app.locals

  • Global Availability: Values stored in app.locals are available throughout the application, which helps reduce the need to repeatedly pass variables between routes or middleware
  • Centralized Configuration: app.locals is ideal for storing application-level settings like environment configurations, application-specific data, or frequently used values
  • Ease of Use: It simplifies the management of shared variables across different parts of your application, making your code more maintainable

Conclusion

The app.locals object in Express.js is a powerful feature for managing global variables and application configurations. By storing values centrally in app.locals, developers can easily access and manage data across different parts of the application, improving code readability and reducing redundancy


Next Article

Similar Reads