在出口的NodeJS VAR其中包含连接到MySQL的创建
我需要出口这在我的项目的其他文件中使用它。谢谢。
我试图用module.exports导出和导入它与其他文件所需要的,但是当它失败。 MySQL是没有定义。
var myConnection = mysql.createConnection({
host: "192.168.1.50",
user: "pi",
password: "root"
});
我希望你帮我把这些变量来我的项目的其他部分。
回答如下:Using ES6 imports/exports
要导出您的变量,像这样做
// exportFile.js
export const myConnection = mysql.createConnection({
host: "192.168.1.50",
user: "pi",
password: "root"
});
将其导入另一个文件,使用该
// importerFile.js
import { myConnection } from './exporterFile';
Using require
出口这样使用module.exports
// exportFile.js
module.exports = mysql.createConnection({
host: "192.168.1.50",
user: "pi",
password: "root"
});
将其导入另一个文件,使用该
// importerFile.js
const myConnection = require('./exporterFile');