module.exports---require案例
//config.js
module.exports = {
userName:'zhangdan',
say:function () {
return 'hello';
}
}
exports.userName = "tom";
exports.say = function () {
return "hello";
}
var config = require('config.js')
var username= config.userName
export--import案例
//config.js
var apiServer = 'http://127.0.0.1:3000'
function add(a,b) {
return a+b;
}
export {apiServer,add}
import {apiServer,add} from '../../config'
console.log(apiServer);
console.log(add(1, 2));
本文通过具体示例,深入探讨了Node.js中模块的导出与导入机制,包括使用module.exports和exports的区别,以及ES6的export和import语法。通过对比不同方式下模块的使用,帮助读者更好地理解Node.js模块系统的运作原理。
1413

被折叠的 条评论
为什么被折叠?



