【问题标题】:Express has no method configure errorExpress 没有方法配置错误
【发布时间】:2014-04-07 18:36:31
【问题描述】:

我正在尝试开始使用 MEAN 堆栈。我正在关注本教程:link

我已经完成了测试我们的服务器部分。这里

// modules =================================================
var express = require('express');
var app     = express();
var mongoose= require('mongoose');

// configuration ===========================================

// config files
var db = require('./config/db');

var port = process.env.PORT || 8080; // set our port
mongoose.connect(db.url); // connect to our mongoDB database (uncomment after you enter in your own credentials in config/db.js)

app.configure(function() {
    app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
    app.use(express.logger('dev'));                     // log every request to the console
    app.use(express.bodyParser());                      // have the ability to pull information from html in POST
    app.use(express.methodOverride());                  // have the ability to simulate DELETE and PUT
});

// routes ==================================================
require('./app/routes')(app); // configure our routes

// start app ===============================================
app.listen(port);                                       // startup our app at http://localhost:8080
console.log('Magic happens on port ' + port);           // shoutout to the user
exports = module.exports = app;                         // expose app

当我跑步时

nodemon server.js

我收到此错误

app.configure(function() {
^
TypeError: Object function (req, res, next) {
 app.handle(req, res, next);
} has no method 'configure'
 at Object.<anonymous> (C:\Users\Yuksel\Desktop\node\test\server.js:14:5)
 at Module._compile (module.js:456:26)
 at Object.Module._extensions..js (module.js:474:10)
 at Module.load (module.js:356:32)
 at Function.Module._load (module.js:312:12)
 at Function.Module.runMain (module.js:497:10)
 at startup (node.js:119:16)
 at node.js:902:3
5 Mar 17:27:20 - [nodemon] app crashed - waiting for file changes before startin
g...

它只是说应用程序没有方法配置(我猜)。但是当我删除配置部分并再次运行它时,它可以工作。(这意味着应用程序具有 .listen 方法,因此它是一个快速对象。)

我已经尝试过使用 node 和 nodemon。我想不通。感谢您的宝贵时间。

【问题讨论】:

  • 因为快递4

标签: javascript node.js express mean-stack


【解决方案1】:

Tom 在他的博文 new-features-node-express-4 中提供了如何从使用 express 3.x 版本中的 app.configure 转换为在 express 版本 4.0 中删除它的示例。

为方便起见,我在下面添加了代码示例。

版本 3.x

// all environments
app.configure(function(){
  app.set('title', 'Application Title');
})

// development only
app.configure('development', function(){
  app.set('mongodb_uri', 'mongo://localhost/dev');
})

// production only
app.configure('production', function(){
  app.set('mongodb_uri', 'mongo://localhost/prod');
})

4.0 版

// all environments
app.set('title', 'Application Title');

// development only
if ('development' == app.get('env')) {
  app.set('mongodb_uri', 'mongo://localhost/dev');
}

// production only
if ('production' == app.get('env')) {
  app.set('mongodb_uri', 'mongo://localhost/prod');
}

【讨论】:

  • 谢谢伙计。感谢您展示如何在 4.0+ 版本中使用 express
  • 是的,这个答案对我帮助很大。很高兴您为我们说明了这一点
【解决方案2】:

configure 方法已从 express 版本 4.0.0(包括 4.0.0-rc2)中删除。查看https://github.com/strongloop/express/blob/master/History.md#400--2014-04-09的更新日志

【讨论】:

  • 只是为了添加到这个答案 - 这就是问题所在,因为在 OP 发布的教程中,package.json 中有这个:“express”: “最新的” 这将使这个答案正确。只是说明为什么“最新”不是一个好用的版本
  • 我应该改用什么方法?
  • @SuperioREX 不要用新方法替换 - 只需执行里面的代码。 stackoverflow.com/questions/18637148/…
猜你喜欢
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2023-03-29
  • 2012-11-09
相关资源
最近更新 更多