node mysql单例_nodejs 模块引用的单例性

1.测试项目结构如下:

c52401d6b216454f4daecb47cd8d0bef.png

2运行:

node first.js

3结果如下:

0378e9a3ccb4574130af86401f805f0f.png

4代码:

first.js

console.info("first.js require module");

var mod = require('./module');

console.info("first.js show module::————");

console.info(mod);

console.info("first.js require second before add var2");

var second = require('./second');

console.info("first.js add var2");

mod.var2 = 'var2';

console.info("first.js show module::————");

console.info(mod);

console.info("first.js require second.js");

var second = require('./second');

console.info("first.js run exported second");

second();

console.info("first.js show moddule::————");

console.info(mod);

second.js:

var mod2 = require('./module');

console.info("second.js require module,and module in second is::————");

console.info(mod2);

exports = module.exports = function () {

console.info("second show module in exports::————");

console.info(mod2);

console.info('second add var3');

mod2.var3 = 'var3';

};

module/index.js:

console.info('module.exports var1');

exports = module.exports= {

var1 : 'var1'

};

//以模块默认入口而非index文件引入时对外不共享var1

// exports = {

// var1 : 'var1'

// };

5.总结

不同文件require第三方模块时,是以引用传递的方式引入的,彼此共享模块的变量值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的第一个问题是关于 node.js 和 MySQL 插入语句的语法错误提示,是吗? 首先,你需要确保你的 MySQL 语句的语法是正确的。MySQL 插入语句的基本语法如下: ``` INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 其中,`table_name` 是你要插入数据的表的名称,`column1, column2, column3` 是你要插入的列的名称,`value1, value2, value3` 是你要插入的值。 在 node.js 中,你可以使用 `mysql` 模块来连接和操作 MySQL 数据库。下面是一个示例代码,演示如何使用 `mysql` 模块来插入数据到 MySQL 数据库中: ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = "INSERT INTO customers (name, address) VALUES ('John Doe', 'Highway 71')"; connection.query(sql, (err, result) => { if (err) throw err; console.log('1 record inserted'); connection.end(); }); }); ``` 在上面的代码中,我们创建了一个 `mysql` 连接对象 `connection`,并在连接成功后,使用 `connection.query()` 方法来执行 MySQL 插入语句。 如果你使用类似上面的代码来执行 MySQL 插入语句,但是仍然遇到语法错误提示,那么你需要检查你的 MySQL 语句是否正确。你可以尝试在 MySQL 命令行中执行你的 MySQL 语句,以确保它没有语法错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值