欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Nodejs模块的调用操作实例分析

程序员文章站 2022-03-21 14:05:30
本文实例讲述了nodejs模块的调用操作。分享给大家供大家参考,具体如下: user.js //构造方法 function user(id, name, ag...

本文实例讲述了nodejs模块的调用操作。分享给大家供大家参考,具体如下:

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程序设计有所帮助。