2014-11-03 41 views
1

我想要構建我的第一個節點應用程序。我的app.js文件如下所示。我想通過執行'app = require('app')'來從另一個模塊訪問它。然後我想要訪問app.app,app.dbConn和app.models創建一個javascript對象並綁定這個

問題是,當我需要此模塊時,app.models不在結果對象上。

var express = require('express'); 
var path = require('path'); 
var orm = require('orm'); 

var settings = require('./config/settings'); 
var mainRouter = require('./config/routes'); 
var environment = require('./config/environment'); 
var db = require('./config/db'); 
var auth = require('./modules/auth'); 

module.exports = new function(){ 
    this.app = express(); 

    // middlewares must be added in order - start with the basics 
    environment(this.app); 
    if (process.env.TESTING) { dbSettings = settings.dbTesting; } 
    else { dbSettings = settings.db; } 

    // add models to the request early in the middleware chain 
    this.dbConn = orm.connect(dbSettings, function(err){ 
     if (err) return console.error('DB Connection error: ' + err); 
     else{ 
     this.models = db.init(this.dbConn); 
     this.app.use(function(req,res,next){ 
      req.models = this.models; 
      next(); 
     }); 

     passport = auth.init(this.models); 
     authRouter = auth.router(passport) 

     this.app.use('/users', authRouter); 
     this.app.use(mainRouter); 
     } 
    }.bind(this)); 
    this.app.listen(settings.port); 
    console.log('Server started... listening on port ' + settings.port) 
} 

實現我想要的是一個函數,它接受一個回調,在我重寫我的代碼從而結束的唯一方法:

var express = require('express'); 
var path = require('path'); 
var orm = require('orm'); 

var settings = require('./config/settings'); 
var mainRouter = require('./config/routes'); 
var environment = require('./config/environment'); 
var db = require('./config/db'); 
var auth = require('./modules/auth'); 

var app; 

module.exports = function(cb){ 
    app = express(); 

    // middlewares must be added in order - start with the basics 
    environment(app); 
    if (process.env.TESTING) { dbSettings = settings.dbTesting; } 
    else { dbSettings = settings.db; } 

    // add models to the request early in the middleware chain 
    dbConn = orm.connect(dbSettings, function(err){ 
    if (err) return console.error('DB Connection error: ' + err); 
    else{ 
     models = db.init(dbConn); 
     app.use(function(req,res,next){ 
     req.models = models; 
     next(); 
     }); 

     passport = auth.init(this.models); 
     authRouter = auth.router(passport) 

     app.use('/users', authRouter); 
     app.use(mainRouter); 

     cb({ 
     dbConn: dbConn, 
     app: app, 
     models: models 
     }); 
    } 
    }); 
} 

if (!process.env.TESTING) { 
    module.exports(function(server){ 
    server.app.listen(settings.port); 
    console.log('Server started... listening on port ' + settings.port) 
    }); 
} 

回答

1

app.models只有一次數據庫連接完成的定義:需要模塊後,您不能使用它的時候了。您應該提供一個入口點,該入口點接受一個連接準備就緒後調用的函數,並在orm.connect回調中調用此函數。

即使是「服務器啓動」的消息是有點誤導,因爲它的顯示之前的服務器實際上可以做任何事情,因爲傳遞給orm.connect函數沒有被調用呢。

0

app.models是從來沒有定義,因此這是從來沒有可用。

相反,嘗試

app.set('models', db.init(this.dbConn);