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
我有几个变量,比如app key,我需要访问所有文件,我需要有一些初始化程序,它将在一个地方初始化我的所有常量,并且应该能够访问整个应用程序?有关如何在节点中实现此初始化程序的任何建议。我不想要文件
3 个解决方案
#1
2
An ideal way to do , preventing accidental damage to global configurations,
一个理想的方法,防止意外损坏全局配置,
applicationConstants.js
applicationConstants.js
module.exports = { app_host : "example.com" , app_port:8080}
Object.freeze(module.exports); // ensure protection against modification
app.js
app.js
var appConstants = require('path_to_applicationConstants.js');
Object.defineProperty(global, "appConstants", {
enumerable: false,
configurable: false,
writable: false,
value: appConstants
});
#2
2
The Globals object should sort you out: http://nodejs.org/api/globals.html#globals_global
Globals对象应该排除你:http://nodejs.org/api/globals.html#globals_global
#3
-1
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.
如果您希望在应用程序中存储一些变量,那么您应该使用应用程序的共享首选项。
#1
2
An ideal way to do , preventing accidental damage to global configurations,
一个理想的方法,防止意外损坏全局配置,
applicationConstants.js
applicationConstants.js
module.exports = { app_host : "example.com" , app_port:8080}
Object.freeze(module.exports); // ensure protection against modification
app.js
app.js
var appConstants = require('path_to_applicationConstants.js');
Object.defineProperty(global, "appConstants", {
enumerable: false,
configurable: false,
writable: false,
value: appConstants
});
#2
2
The Globals object should sort you out: http://nodejs.org/api/globals.html#globals_global
Globals对象应该排除你:http://nodejs.org/api/globals.html#globals_global
#3
-1
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.
如果您希望在应用程序中存储一些变量,那么您应该使用应用程序的共享首选项。