首页  >  文章  >  web前端  >  Nodejs模块的调用操作方法解析

Nodejs模块的调用操作方法解析

云罗郡主
云罗郡主原创
2019-01-21 14:02:283154浏览

这篇文章主要介绍了Nodejs模块的调用操作,结合实例形式分析了nodejs模块的定义与调用相关操作技巧,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。【推进教程:jQuery视频教程

User.js

//构造方法
function User(id, name, age) {
  this.id = id;
  this.name = name;
  this.age = age;
  this.enter = function () {
    console.log(this.name + "进入国家图书馆");
  }
}
/*
function User() {
  this.id;
  this.name;
  this.age;
  this.enter = function() {
    console.log(this.name + "进入图书馆");
  }
}
module.exports = User;

Teacher.js

var User = require('./User');
function Teacher(id, name, age) {
  User.apply(this, [id, name, age]);//类的继承
  this.teach = function(res) {
    res.write(this.name + "讲课");
  }
}
module.exports = Teacher;

modalcall_1.js    

//----------------------n3_modalcall.js模块的调用-------------
var http = require('http');
var User = require('./model/User');
http.createServer(function (request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8'
  });
  if (request.url !== "/favicon.ico") { //清除第2此访问
    user = new User(1, "jack", 20);
    //user.id = 1;
    //user.name = "张三";
    //user.age = 20;
    user.enter();
    response.end('');
  }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

modalcall_2.js

//----------------------n3_modalcall.js-------------
var http = require('http');
var Teacher = require('./model/Teacher');
http.createServer(function(request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8'
  });
  if(request.url !== "/favicon.ico") { //清除第2此访问
    teacher = new Teacher(1, "JackLi", 20);
    teacher.enter();
    teacher.teach(response);
    response.end('');
  }//
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');


以上是Nodejs模块的调用操作方法解析的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn