express里面的app.configure作用
 发布于 13 年前  作者 xiaxiaokang  16961 次预览  最后一次回复是 11 年前  来自  

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

3 回复
xiongliding

以下摘自 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');
}

是一个效果。