带有ExpressJS的Nodejs包括外部js代码文件供使用

时间:2016-10-08 23:33:26

标签: javascript node.js express

在这里学习NodeJS ......我有这个非常基本的"类"文件我放入外部JS文件。如何使我的所有路由文件和/或其他外部js文件(我也想加载)看到这个?

/classes/music/usermanager.js

function User(){
    this.email = "";
    this.password = "";
    this.userid = "";
}

function UserManager()
{
    this.user = new User();
}

UserManager.prototype.login = new function (email, password){
    var db = new DbManager();
    db.Open();
    params = {
        $email : email,
        $password : password
    }
    db.serialize(function(){
        db.get("SELECT * FROM Users WHERE email = $email and password =     $password", params, function(err,row) {
        if (!err && row) {
            //log in passed
            this.user.userid = row.user.id;
            this.user.email = row.user.email;
            this.user.password = row.user.password;
            return this.user;
        }
        else if (!err) {
            //log in failed log event
            return null;
        }
        else {
            //error happened through out an event to log the error
            return null;
        }
    });
});
}

/config/strategies/local.strategy.js 我将如何在另一个文件中使用它的示例。

var passport = require('passport');
LocalStrategy = require('passport-local').Strategy;

module.exports = function () {
    passport.use(new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    },
    function (email, password, done) {
        //vadliating user here
        var userManager = new UserManager();
        var user = userManager.login(email, password);
        done(null, user);
    }));
};

我尝试在我的app.js文件中执行类似这样的操作,并且正在编译说明"抛出新的TypeError(' app.use()需要中间件函数');&#34 ;。我可以发誓我读到了你可以做到的地方,但现在无法找到资源。

/asp.js

/*load external js library files for application use*/
app.use ("./classes/music/:path*");

感觉我在尝试组织nodejs / expressjs项目时仍然缺少基本的理解。

2 个答案:

答案 0 :(得分:0)

您实际上在一个示例中使用了答案。您可以使用module.exports = whatever;使模块可用。这意味着您可以导出任何字符串,对象或"类"。因此,您可以通过将UserManager添加到文件的末尾(或实际上在任何地方)来导出module.exports = UserManager;

var UserManager = require('./usermanager.js');

并使用其相对文件路径在另一个文件管理器中要求它。所以如果它在同一个目录中。

var UserManager = require('../../classes/music/usermanager.js');

因此对于需要UserManager的local.strategy.js文件,它看起来像:(如果你的文件路径都在同一根目录下)

{{1}}

答案 1 :(得分:0)

<强> /classes/music/usermanager.js

  // Working for me, try it, hope this may help you

  var model = module.exports; 

   function User() {
    this.email = "";
    this.password = "";
    this.userid = "";  
     }

 function UserManager() {
    this.user = new User();

   }

UserManager.prototype.login = function(email, password) {

      var db = new DbManager();
    db.Open();
    params = {
        $email : email,
        $password : password
    }
    db.serialize(function(){
        db.get("SELECT * FROM Users WHERE email = $email and password =     $password", params, function(err,row) {
        if (!err && row) {
            //log in passed
            this.user.userid = row.user.id;
            this.user.email = row.user.email;
            this.user.password = row.user.password;
            return this.user;
        }
        else if (!err) {
            //log in failed log event
            return null;
        }
        else {
            //error happened through out an event to log the error
            return null;
        }
    }); 

   });     
   }; 


model.login = function (email, password){    
var usrmng = new UserManager(); 
    return usrmng.login(email, password); 
  };

<强> /config/strategies/local.strategy.js

 var usermanager = require('./usermanager'); // path to ur file


  var passport = require('passport');
  LocalStrategy = require('passport-local').Strategy;

  module.exports = function () {
  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
   },
  function (email, password, done) {
    //vadliating user here
    //var userManager = new UserManager();
   var use = usermanager.login(email, password);
    done(null, user);
   }));
 };