gpt4 book ai didi

javascript - 在 NodeJS 中包含 Javascript 文件,如 PHP

转载 作者:行者123 更新时间:2023-12-03 22:40:32 25 4
gpt4 key购买 nike

在 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
});

我正在尝试将数据库连接代码放入单独的文件 但是当我这样做并尝试返回所需的连接值时,连接似乎永远不会正确导入,并且我收到一个错误,即在连接上找不到导入函数?

对于这个例子,我将如何做到这一点? 如果 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");因为它再也找不到导入功能了。

最佳答案

我在评论中使用 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
});

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

关于javascript - 在 NodeJS 中包含 Javascript 文件,如 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48354017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com