2014-03-05 5 views
20

Ich versuche, mit dem MEAN-Stack zu beginnen. Und ich folge diesem Tutorial: linkExpress hat keine Methode konfigurieren Fehler

Ich habe es bis zum Test Unser Server Abschnitt gemacht.

hier
// 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 

Wenn ich laufen

nodemon server.js 

Ich erhalte diese Fehler

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... 

Es sagt einfach App keine Methode configure hat (ich glaube). Aber wenn ich das konfiguriere Teil lösche und es wieder leite, funktioniert es. (Diese mittlere App hat die .listen-Methode, also ist es ein Express-Objekt.)

Ich habe mit beiden Knoten und nodemon versucht. Und ich konnte es nicht herausfinden. Vielen Dank für Ihre Zeit.

+0

es ist, weil der Express 4 –

Antwort

22

Die Konfigurationsmethode wurde ab Express 4.0.0 (einschließlich 4.0.0-rc2) entfernt. Sehen Sie das Changelog unter https://github.com/strongloop/express/blob/master/History.md#400--2014-04-09

+3

Nur auf diese Antwort hinzufügen - das ist das Problem im Tutorial, weil die OP geschrieben, die * * package.json ** hat folgendes: ** "express": "neuste" **, die diese Antwort korrekt machen würde. Zeigt nur, warum "neueste" ist keine gute Version zu verwenden – Ian

+4

welche Methode sollte ich stattdessen verwenden? – SuperioREX

+5

@SuperioREX Ersetzen Sie nicht mit einer neuen Methode - führen Sie einfach den Code darin aus. http://stackoverflow.com/questions/18637148/using-app-configure-in-express – Ian

47

Tom in seinem Blog-Beitrag new-features-node-express-4 bietet Beispiele für die Konvertierung von der Verwendung von app.configure in Express-Version 3.x, um es in Express-Version 4.0 zu entfernen.

Der Einfachheit halber habe ich das folgende Codebeispiel hinzugefügt.

Version 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'); 
}) 

Version 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'); 
} 
+2

danke Mann. vielen Dank, dass Sie zeigen, wie Express gemäß der Version 4.0+ zu verwenden ist. – STEEL

+0

Ja, diese Antwort hat mir enorm geholfen. Ich bin froh, dass du es für uns buchstabiert hast –

Verwandte Themen