跳到主要内容
CNode

express里面的app.configure作用

社区
Xxiaxiaokang发布于 14 年前最后回复 12 年前
3171640

看了官方文档还是没看懂 app.set放在外面和放在app.configure里面有什么区别

查看回复

回复 (3)

X
xiongliding#114 年前

以下摘自 express 3.0 的 文档

app.configure([env], callback)

Conditionally invoke callback when env matches app.get('env'), aka process.env.NODE_ENV. This method remains for legacy reason, and is effectively an if statement as illustrated in the following snippets. These functions are not required in order to use app.set() and other configuration methods.

里面说 app.configure 是以前的版本遗留下来的,完全可以用条件判断语法取代。

文档里还举例说明了:

app.configure(function() {
  app.set('title', 'My Application');
});

app.set('title', 'My Application');

是等价的,都是对所有环境有效。而

app.configure('development', function(){
  app.set('db uri', 'localhost/dev');
})

if ('development' == app.get('env')) {
  app.set('db uri', 'localhost/dev');
}

是一个效果。

参与回复

登录后即可参与回复。登录