简体   繁体   English

使用node.js和CoffeeScript访问全局变量

[英]Accessing global variables with node.js and CoffeeScript

I'm trying to declare global variables for my applications that I'm writing using Node.js and CoffeeScript. 我正在尝试为我正在使用Node.js和CoffeeScript编写的应用程序声明全局变量。 So I'm declaring it in a common file that is concatenated to both applications after compilation. 所以我在一个公共文件中声明它,它在编译后连接到两个应用程序。 In that file I have for example: 在那个文件中我有例如:

root = exports ? this
root.myVariable = 300

So my first application is a HTML one. 所以我的第一个应用程序是HTML。 When I try to access this variable, for example by 当我尝试访问此变量时,例如

console.log myVariable

There is no problem with it. 它没有问题。 But my other application is a server application lauched by node command and I cannot access that variable in that application. 但我的其他应用程序是由node命令启动的服务器应用程序,我无法访问该应用程序中的该变量。 I tried: 我试过了:

console.log root.myVariable
console.log myVariable

With first line I'm getting 'undefined' printed (so it looks that root is defined) and with the second one, I'm getting ReferenceError - myVariable is undefined. 第一行我打印'未定义'(所以它看起来定义了root),而第二行,我得到ReferenceError - myVariable是未定义的。

So how can I access this variable? 那么我该如何访问这个变量呢?

Here is an output code in Javascript that I get, I guess it might be helpful: 这是我得到的Javascript中的输出代码,我想它可能会有所帮助:

(function() {
  var root, _ref;

  root = (_ref = typeof module !== "undefined" && module !== null ? module.exports : void 0) != null ? _ref : this;

  root.myVariable = 300;

}).call(this);

(function() {

  console.log(root.myVariable);

  console.log(myVariable);

}).call(this);

You're close, but you need to change things just a little bit 你很亲密,但你需要稍微改变一下

# config.coffee
module.exports =
  foo: "bar"
  hello: "world"
  db:
    user: naomik
    pass: password1

# lib/a.coffee
config = require "../config"

# lib/b.coffee
config = require "../config"

# lib/db.coffee
dbconfig = require("../config").db

Client and server JavaScript (or CoffeeScript ) works differently. 客户端和服务器JavaScript (或CoffeeScript )的工作方式不同。 So, its a really difficult to write a module that'll work in both applications. 因此,编写一个可以在两个应用程序中运行的模块真的很难。

There is a lot of libraries to solve this problem, like RequireJS and Browserify . 有很多库可以解决这个问题,比如RequireJSBrowserify

But I have two simpler suggestions for your problem. 但我对你的问题有两个更简单的建议。


First one is to use JSON to store your global constants. 第一个是使用JSON来存储全局常量。 On the server side you can simply require you JSON file: 在服务器端,您只require JSON文件:

root = require './config.json'

On the client side you may either parse it manually or serve it as pjson . 在客户端,您可以手动解析它,也可以将其作为pjson


My second suggestion is to write really simple module that'll be compatible with both your applications. 我的第二个建议是编写一个非常简单的模块,它将与你的两个应用程序兼容。 It'll look something like this: 它看起来像这样:

root = 
  myVariable: 300
  myOtherVariable: 400

modulte.exports = root if module?.parent?

This code should be compatible with both node.js require function and browser <script> tag. 此代码应与node.js require函数和browser <script>标记兼容。

Update: 更新:

I just reread you question and realized, that you've did almost as I suggested. 我只是重读了你的问题并意识到,你几乎按照我的建议做了。 But your code looks fine to me. 但是你的代码看起来很好。 You may try to use module.export instead of its alias exports , it may help: 您可以尝试使用module.export而不是其别名exports ,它可能会有所帮助:

root = modulte?.exports ? this
root.myVariable = 300

But, as I said, your code looks fine to me as well. 但是,正如我所说,你的代码对我来说也很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM