sunny03 发表于 2017-2-24 11:25:01

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
页: [1]
查看完整版本: NodeJS写模块和引入模块的例子