Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Accessing Global Variables within Routes in Express

I'm still new to Express, so please bear with me.

What I'm trying to do is store some of my environmental variables in a global variable such that they can be accessed in any route without having to redeclare the variables within each route.

Note that I used the Express Generator to set up the project scaffolding.

Here's a stripped-down example of what I'm trying to do:

App.js

const express = require('express');

// Load Routes
const inventoryRouter = require('./src/routes/inventory');

// Instantiate Express/Assign App Var
const app = express();

// Set Global Env Vars
app.set('DevUrl', process.env.Dev_URL || null);

// Use Routes
app.use('/inventory', inventoryRouter);

module.exports = app;

Inventory.js

const express = require('express');
const router = express.Router();

// Require Inventory Controller
const inventoryController = require('../controllers/inventoryController');

// Inventory Routes
router.get('/', inventoryController.get_inventory);
router.post('/', inventoryController.update_inventory);

module.exports = router;

InventoryController.js

const router = express.Router();

// Store Vars
const LocalRouteVariable = app.get('DevUrl') + 'Some String';

// Rest of Controller Code ...

Question "app" is not defined in the controller. So, my question is, how can I get the value of "DevUrl"?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!