NodeJS写模块和引入模块的例子


nodejs自学.js

function hello(){
console.log("hello world");
}

function s(){
console.log("this is a ew");
}

function add(a, b){
return a+b;
}

exports.hello = hello;//留出接口
exports.s = s;
exports.add = add;

test.js

//加载模块
var app = require("./nodejs自学");//./表示当前目录,也可以填"./nodejs自学.js"
app.hello();
app.s();
console.log(app.add(1,3));

 

在终端中 node test.js 

结果:

hello world

this is a ew

4


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM