在NodeJS中包含Javascript文件,如PHP

时间:2018-01-20 08:20:49

标签: javascript node.js sequelize.js

在PHP中,您可以包含一个文件,并复制所有函数变量

我知道要包含的require命令和module.export概念,但它似乎只允许你包含函数?

我有一些看起来像这样的代码。此代码目前有效。

var argv = require('minimist')(process.argv.slice(2));
//console.dir(argv);

//Change connection settings based on stage variable.  Assume localhost by default.
var dbname = argv['stage'] ? argv['stage']+"_db"                              : 'localdb';
var dbuser = argv['stage'] ? process.env.RDS_USERNAME                         : 'admin';
var dbpass = argv['stage'] ? process.env.RDS_PASSWORD                         : 'local123';
var dbhost = argv['stage'] ? "database-"+argv['stage']+".whatever.com"  : 'localhost';

var Sequelize = require('sequelize');
var connection = new Sequelize(dbname,dbuser,dbpass, {
  dialect: 'mysql',
  operatorsAliases: false, //This gets rid of a sequelize deprecated warning , refer https://github.com/sequelize/sequelize/issues/8417
  host: dbhost
});

//Import all the known models for the project.
const Users = connection.import(__dirname + "/models/users.js");

connection.sync().then(function() {
  process.exit();//close the nodeJS Script
});

我正在尝试将数据库连接代码放入单独的文件但是当我这样做并尝试返回require的连接值时,连接似乎永远不会正确导入,我得到在连接上找不到导入功能的错误?

我如何为此示例执行此操作?如果Include像PHP一样工作,我只需要包含该文件,连接变量就可以正常工作。我还是NodeJS的新手,所以不确定语法。我环顾四周其他答案,但我不明白它是如何运作的。

我试图解决这个问题就像这样。

    module.exports = function(){
    var argv = require('minimist')(process.argv.slice(2));
    //console.dir(argv);

    //Change connection settings based on stage variable.  Assume localhost by default.
    var dbname = argv['stage'] ? argv['stage']+"_db"                              : 'localdb';
    var dbuser = argv['stage'] ? process.env.RDS_USERNAME                         : 'admin';
    var dbpass = argv['stage'] ? process.env.RDS_PASSWORD                         : 'local123';
    var dbhost = argv['stage'] ? "database-"+argv['stage']+".whatever.com"  : 'localhost';

    var Sequelize = require('sequelize');
    var connection = new Sequelize(dbname,dbuser,dbpass, {
      dialect: 'mysql',
      operatorsAliases: false, //This gets rid of a sequelize deprecated warning , refer https://github.com/sequelize/sequelize/issues/8417
      host: dbhost
    });
    return connection;

}

然后我会说connection = require(./config.js);之类的内容,错误会出现在这一行const Users = connection.import(__dirname + "/models/users.js");,因为它再也找不到导入函数了。

1 个答案:

答案 0 :(得分:0)

我在评论中使用PitaJ的建议修改了我的答案,这是我修改过的模块文件。

exports.getSequelizeConnection = function(stage){
      var argv = require('minimist')(process.argv.slice(2)); //If this file is being used in a script, this will attempt to get information from the argument stage passed if it exists
      var connection;

      //Change connection settings based on stage variable.  Assume localhost by default.
      var dbname = argv['stage'] ? argv['stage']+"_db"                              : 'localdb';
      var dbuser = argv['stage'] ? process.env.RDS_USERNAME                         : 'admin';
      var dbpass = argv['stage'] ? process.env.RDS_PASSWORD                         : 'local123';
      var dbhost = argv['stage'] ? "database-"+argv['stage']+".whatever.com"  : 'localhost';

      //If state variable used during require overide any arguments passed.
      if(stage){
        dbname = stage+"_db";
        dbuser = process.env.RDS_USERNAME
        dbpass = process.env.RDS_PASSWORD
        dbhost = "database-"+stage+".poolservice123.com"
      }

      var Sequelize = require('sequelize');
      var connection = new Sequelize(dbname,dbuser,dbpass, {
        dialect: 'mysql',
        operatorsAliases: false, //This gets rid of a sequelize deprecated warning , refer https://github.com/sequelize/sequelize/issues/8417
        host: dbhost
      });
      return connection;
    }

然后,我可以在我的其他文件中简单地将其称为...

var config = require('./sequelize-config.js');
connection = config.getSequelizeConnection();

//Import all the known models for the project.
const Users   = connection.import(__dirname + "/models/Users.js");
const Events  = connection.import(__dirname + "/models/Calls.js");

//Alter table as needed but do NOT force the change.  If an error occurs we will fix manually.
connection.sync({ alter: true, force: false }).then(function() {
  process.exit();//close the nodeJS Script
});

这种方法的优点是我现在可以为命令行脚本和使用数据库的非命令行脚本使用相同的配置文件。