问题描述
我发现一些代码,他们设置Express而不使用 app.configure
,我想知道,使用 app.configure
没有环境说明符,不使用它?
I found some code where they set up Express without using app.configure
and I was wondering, what's the difference between using app.configure
without an environment specifier and not using it?
换句话说,这有什么区别:
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')));
}
和这个:
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')));
谢谢。
推荐答案
根据文档保留原来的原因
在你的例子中,两段代码根本没有区别。
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
2015年更新
@IlanFrumer指出,在Express 4.x中删除了app.configure。如果你遵循了一些过时的教程,并想知道为什么它不起作用,你应该删除 app.configure(function(){...}
。像这样:
@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:
var express = require('express');
var app = express();
app.use(...);
app.use(...);
app.get('/', function (req, res) {
...
});
这篇关于在express中使用app.configure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!