我对 node.js 中的模块感到困惑

I'm confused with the module in node.js

一共有三个模块:a.js,b.js,c.jsa.js是导出一个数据库连接,b.js,c.js是导入连接,就像两个不同的http路由一样,所以连接module(a.js)会被导入两次,是不是代表驱动会连接数据库两次?

我试过这样: test.js

let a = 20;
let b = 30;
console.log('I am test');
let obj = {
    a,
    b
}
module.exports = obj;

en.js

let obj = require('./test');

console.log('I am en.js')

module.exports = obj;

hi.js

let obj = require('./test');
let obj2 = require('./en');

console.log(obj2)

运行 hi.js 结果:

I am test
I am en
{a:20,b:30}

en.js和hi.js都导入了test.js,从结果看test.js好像只有运行一次,为什么呢?希望得到您的帮助。

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

您可以在此处阅读更多内容

https://nodejs.org/api/modules.html#modules_caching