V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
skynothing
V2EX  ›  Node.js

nodejs项目如何组织公共常量

  •  
  •   skynothing · 2013-03-07 10:08:07 +08:00 · 5134 次点击
    这是一个创建于 4067 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如项目使用 express 模块,有一个常数值需要在多个代码文件用到,该如何放置?
    7 条回复    1970-01-01 08:00:00 +08:00
    ljbha007
        1
    ljbha007  
       2013-03-07 12:53:14 +08:00
    app.set("foo", "bar");
    var foo = app.get("foo");
    ljbha007
        2
    ljbha007  
       2013-03-07 12:54:48 +08:00   ❤️ 1
    或者
    app.configure(function(){
    app.set("foo", "bar");
    });
    var foo = app.get("foo");
    aveline
        3
    aveline  
       2013-03-07 12:56:47 +08:00
    global.the_const = 42;

    这样子最方便了 ...
    2code
        4
    2code  
       2013-03-07 12:57:27 +08:00
    config.js
    module.exports = {
    foo:'bar'
    }

    require('./config.js')
    NemoAlex
        5
    NemoAlex  
       2013-03-07 13:25:00 +08:00
    如果是模块化的功能,4楼的方式 + 1
    常量的话一般放到类似 config.js 之类的文件里就好了
    NemoAlex
        6
    NemoAlex  
       2013-03-07 13:30:29 +08:00
    @NemoAlex 我语言组织能力挂了。这句话实际上是想说:常量可以这样存,模块的功能也可以这样存。
    halfblood
        7
    halfblood  
       2013-03-07 21:13:52 +08:00
    4楼 +1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3264 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:47 · PVG 21:47 · LAX 06:47 · JFK 09:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.