express里面的app.configure作用
发布于 12 年前 作者 xiaxiaokang 16284 次浏览 最后一次编辑是 8 年前

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

3 回复

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

是一个效果。

非常感谢!

回到顶部