node.js 在 express 中使用 app.configure

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18637148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:31:15  来源:igfitidea点击:

Using app.configure in express

node.jsexpress

提问by JayC

I found some code where they set up Express without using app.configureand I was wondering, what's the difference between using app.configurewithout an environment specifier and not using it?

我发现了一些代码,他们在不使用的情况下设置了 Express app.configure,我想知道,在app.configure没有环境说明符的情况下使用和不使用它有什么区别?

In other words, what's the difference between this:

换句话说,这之间有什么区别:

var app = require(express);

app.configure(function(){
    app.set('port', process.env.PORT || config.port);
    app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
    app.use(express.bodyParser());
    app.use(express.static(path.join(__dirname, 'site')));
}

and this:

和这个:

var app = require(express);

app.set('port', process.env.PORT || config.port);
app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'site')));

Thanks.

谢谢。

回答by Jason Leung

It is optional and remain for legacy reason, according to the doc. In your example, the two piece of codes have no difference at all. http://expressjs.com/api.html#app.configure

根据文档,它是可选的,并且由于遗留原因而保留。在您的示例中,这两段代码根本没有区别。 http://expressjs.com/api.html#app.configure

Update 2015:

2015 年更新:

@IlanFrumer points out that app.configure is removed in Express 4.x. If you followed some outdated tutorials and wondering why it didn't work, You should remove app.configure(function(){ ... }. Like this:

@IlanFrumer 指出在 Express 4.x 中删除了 app.configure。如果您遵循了一些过时的教程并想知道为什么它不起作用,您应该删除app.configure(function(){ ... }. 像这样:

var express = require('express');
var app = express();

app.use(...);
app.use(...);

app.get('/', function (req, res) {
    ...
});