创建一个javascript对象并绑定它

时间:2014-11-03 01:18:41

标签: javascript node.js

我正在尝试构建我的第一个节点应用。我的app.js文件如下所示。我想通过' app = require(' app')'来从另一个模块访问它。然后我想访问app.app,app.dbConn和app.models

问题在于,当我需要此模块时,结果对象上不存在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)
  });
}

2 个答案:

答案 0 :(得分:1)

仅在数据库连接完成后才定义

app.models:在需要模块后,您无法立即使用它。您应该提供一个入口点,该入口点接受一个连接就绪后要调用的函数,并在orm.connect回调中调用此函数。

甚至"服务器也开始了#34;消息有点误导,因为它显示之前服务器实际上可以执行任何操作,因为传递给orm.connect的函数尚未被调用。

答案 1 :(得分:0)

app.models永远不会被定义,因此它永远不可用。

相反,请尝试

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