如何在全局范围内的nodejs中定义const?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20239856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define const in nodejs in global scope?
提问by Kin
I want to separate my app in to the parts to have something like MVC... Currently I figured out exportsworks and how to communicate between different files. The one thing i cant understand is that how to use constants in global scope? Currently i have something like this:
我想将我的应用程序分成几个部分,以获得类似 MVC 的东西......目前我想出了exports工作以及如何在不同文件之间进行通信。我无法理解的一件事是如何在全局范围内使用常量?目前我有这样的事情:
// start.js
const ROOT_DIR = __dirname;
const APP_DIR = ROOT_DIR + '/app/';
const MODULES_DIR = '/usr/local/lib/node_modules/';
const APP_PORT = 4935;
var server = require(APP_DIR + 'server.js');
server.start();
// server.js
exports.start = function() {
var express = require(MODULES_DIR + 'express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require(MODULES_DIR + 'socket.io').listen(server),
fs = require('fs'),
path = require('path');
server.listen(APP_PORT);
app.use(express.static(ROOT_DIR + '/assets'));
app.get('/', function (req, res) {
res.sendfile(ROOT_DIR + '/views/index.html');
});
}
Is it possible to automatically assign this constants to server.jsor i need to pass them as variables?
是否可以自动将此常量分配给server.js或我需要将它们作为变量传递?
回答by Boris Kirov
I think, you need create file with constants and use him as require file in begin a other module.
我认为,您需要使用常量创建文件,并在开始另一个模块时将其用作 require 文件。
File consts.js
文件 consts.js
exports.CONST_1 = 42,
exports.CONST_2 = 123;
In the module where necessary:
在必要的模块中:
var consts = require('path_to_consts.js');
var my_var = consts.CONST_1 + consts.CONST_2;
So all global variables will be in one place
所以所有的全局变量都会在一个地方
回答by Munim
Javascript constants won't work globally across files in Node.js. You need to pass them to the function.
Javascript 常量不会跨 Node.js 中的文件全局工作。您需要将它们传递给函数。
// start.js
const ROOT_DIR = __dirname;
const APP_DIR = ROOT_DIR + '/app/';
const MODULES_DIR = '/usr/local/lib/node_modules/';
const APP_PORT = 4935;
var server = require(APP_DIR + 'server.js');
server.start(MODULES_DIR,APP_PORT,ROOT_DIR);
// server.js
exports.start = function(MODULES_DIR,APP_PORT,ROOT_DIR) {
var express = require(MODULES_DIR + 'express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require(MODULES_DIR + 'socket.io').listen(server),
fs = require('fs'),
path = require('path');
server.listen(APP_PORT);
app.use(express.static(ROOT_DIR + '/assets'));
app.get('/', function (req, res) {
res.sendfile(ROOT_DIR + '/views/index.html');
});
}
回答by vkurchatkin
Object.defineProperty(global, 'MY_CONST', { value : 123 })
Object.defineProperty(global, 'MY_CONST', { value : 123 })
P.S. Please, don't do this
PS请不要这样做
回答by NiRmaL
This is method is same as suggested by @user3040347 but little different.
这种方法与@user3040347 建议的方法相同,但几乎没有什么不同。
Here, you need create file with constants and use him as require file in begin of a module in which you want to use.
在这里,您需要使用常量创建文件,并在要使用的模块的开头将其用作 require 文件。
File consts.js
文件 consts.js
CONST_1 = 42,
CONST_2 = 123;
module.exports = {};
In the module where necessary:
在必要的模块中:
var consts = require('path_to_consts.js');
var my_var = CONST_1 + CONST_2;
//Here you can access directly

