skynothing
V2EX  ›  Node.js

nodejs项目如何组织公共常量

  •  
  •   skynothing · Mar 7, 2013 · 6516 views
    This topic created in 4995 days ago, the information mentioned may be changed or developed.
    例如项目使用 express 模块,有一个常数值需要在多个代码文件用到,该如何放置?
    7 replies  •  1970-01-01 08:00:00 +08:00
    ljbha007
    ljbha007
    Mar 7, 2013
    1
    app.set("foo", "bar");
    var foo = app.get("foo");
    ljbha007
    ljbha007
    Mar 7, 2013❤️1
    2
    或者
    app.configure(function(){
    app.set("foo", "bar");
    });
    var foo = app.get("foo");
    aveline
    aveline
    Mar 7, 2013
    3
    global.the_const = 42;

    这样子最方便了 ...
    2code
    2code
    Mar 7, 2013
    4
    config.js
    module.exports = {
    foo:'bar'
    }

    require('./config.js')
    NemoAlex
    NemoAlex
    Mar 7, 2013
    5
    如果是模块化的功能,4楼的方式 + 1
    常量的话一般放到类似 config.js 之类的文件里就好了
    NemoAlex
    NemoAlex
    Mar 7, 2013
    6
    @NemoAlex 我语言组织能力挂了。这句话实际上是想说:常量可以这样存,模块的功能也可以这样存。
    halfblood
    halfblood
    Mar 7, 2013
    7
    4楼 +1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2234 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 13:55 · PVG 21:55 · LAX 05:55 · JFK 08:55
    ♥ Do have faith in what you're doing.