Nodejs学习笔记-模块

代码:https://github.com/fengchunjian/nodejs_examples/tree/master/modelcall

//vim models/User.js
function User(id, name, age) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.entry = function(res) {
        console.log(this.name + "进入图书馆");
        res.write(this.name + "进入图书馆\n");
    }
}
module.exports = User;
//vim models/Teacher.js
var User = require("./User");
function Teacher(id, name, age) {
    User.apply(this, [id, name, age]);
    this.teach = function(res) {
        console.log(this.name + "老师讲课");
        res.write(this.name + "老师讲课\n");
    }
}
module.exports = Teacher;
//vim modelcall.js
var http = require('http')
var User = require("./models/User");
var Teacher = require("./models/Teacher");
http.createServer(function (request, response) {
    user = new User(1, "张三", 20);
    user.entry(response);
    teacher = new Teacher(2, "李四", 21);
    teacher.entry(response);
    teacher.teach(response);
    response.end();
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

node modelcall.js
Server running at http://127.0.0.1:8000/
张三进入图书馆
李四进入图书馆
李四老师讲课

curl http://127.0.0.1:8000/
张三进入图书馆
李四进入图书馆
李四老师讲课

参考文档

node.js教程3_调用模块
http://edu.51cto.com/center//course/lesson/index?id=124527
nodejs3_模块调用
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=35

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,463评论 18 399
  • 人生最可怕的事情,并不是做错事,或者是错过什么!而是没有方向,根本不知道自己想要的是什么!因为,机会来的时候,你没...
    宣继游阅读 1,490评论 0 0
  • 从未有一首英文歌听得如此动容,好像有人在心底的某个地方层层深入地扒拉着,找寻着,而那些沉积的、被故意掩盖的灰尘和疤...
    一鹿长清阅读 4,023评论 0 4
  • “嘟。。。爸,吃饭了吗?” “吃了,有啥事吗?” “没有什么事啊,就打电话问问” ......,此处停顿7秒左右 ...
    黄小丑阅读 1,720评论 0 0

友情链接更多精彩内容