yednamus
yednamus

Reputation: 583

How do i access global variable across all files

I have few variables like app key which i need to access across all files i need to have something like initializers which will initialize all my constants in one place and should be able to access across the application? any suggestions on how to implement this initializers in node. i don't want to require the file

Upvotes: 0

Views: 1408

Answers (3)

Chandu
Chandu

Reputation: 4561

An ideal way to do , preventing accidental damage to global configurations,

applicationConstants.js

 module.exports = { app_host : "example.com" , app_port:8080}
 Object.freeze(module.exports); // ensure protection against modification

app.js

 var appConstants = require('path_to_applicationConstants.js');
 Object.defineProperty(global, "appConstants", {
    enumerable: false,
    configurable: false,
    writable: false,
   value: appConstants 
});

Upvotes: 3

Slappy
Slappy

Reputation: 4092

The Globals object should sort you out: http://nodejs.org/api/globals.html#globals_global

Upvotes: 2

Muhammad Omer
Muhammad Omer

Reputation: 229

Have a Look On This Code . it will clear the picture in your mind :

    public class MyConstants {

    public static final String PROFILE_ID = "profile_id";

    public static final String EMAIL_ID = "email_id";

}

And If You Want To Have some Variables Stored in your App for life time then you should use Shared Preferences of App.

Upvotes: -1

Related Questions