Nodejs模块如何调用

这篇文章主要介绍了Nodejs模块如何调用,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

成都创新互联公司主要从事成都网站制作、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务大厂,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

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模块如何调用内容对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,遇到问题就找创新互联,详细的解决方法等着你来学习!


当前题目:Nodejs模块如何调用
文章网址:http://myzitong.com/article/ihjgsc.html