node.js调用模块


1、新建调用的js
第一种调用没有初始值的模块
var http = require('http');
var User = require('./module/User');//引入的是user模块 不是user.js
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
user = new User(); //实例化这个模块
user.id = 1;
user.name = "张三";
user.age = 20;
user.enter();
response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
function User(){
this.id;
this.name;
this.age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入
第二种调用初始值的模块
var http = require('http');
var User = require('./module/User');//引入的是user模块 不是user.js
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,"zhangsan",20); //实例化这个模块
user.enter();//执行方法
response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
function User(id,name,age){
this.id = id;
this.name = name;
this.age = age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入
2、一个模块继承另一个模块,然后调用
调用的js
var http = require('http');
var Teacher = require('./module/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,"zhangsan",20);
teacher.enter();//对父类的方法调用
teacher.teach(response);//调用自己的方法
response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
User类
function User(id,name,age){
this.id = id;
this.name = name;
this.age = age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入
Teacher类 并且继承User类
var User = reqire('./User');
function Teacher(id,name,age){
User.apply(this,[id,name,age]);//这句话实现了继承User这个模块,并且初始化,里面的id,name,age 就是Teacher(id,name,age)里面传进来的
this.teach = function(res){
res.write(this.name+"讲课");
}
}
module.exports = Teacher;//导出这个模块/类,让其他js能引入
node.js调用模块的更多相关文章
- node.js基础模块http、网页分析工具cherrio实现爬虫
node.js基础模块http.网页分析工具cherrio实现爬虫 一.前言 说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http.网页分析工具cherri ...
- Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码
(从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...
- Node.js Net 模块
Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/客户端的方法,我们可以通过以下方式引入该模块: var net = require("net") ...
- windows下node.js调用bat
node.js调用bat需要用到Child Processes模块 因为bat是文件,所以需要使用execFile方法 如果指定了cwd,它会切换bat执行的目录,类似cd的功能,如果未指定默认为 ...
- 38..Node.js工具模块---底层的网络通信--Net模块
转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/ ...
- Node.js之模块机制
> 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 常见问题(FAQ)-人机界面快速入门 TC3
右击添加一个PLC项,注意不要用中文 右击VISUs,添加一个视图对象 在POUs中打开MAIN,然后添加代码(定义了一个BOOL和一个INT类型变量) 工具箱中得到一个textfield ...
- 乌云主站所有漏洞综合分析&乌云主站漏洞统计
作者:RedFree 最近的工作需要将乌云历史上比较有含金量的漏洞分析出来,顺便对其它的数据进行了下分析:统计往往能说明问题及分析事物的发展规律,所以就有了此文.(漏洞数据抓取自乌云主站,漏洞编号从1 ...
- Java并发包——Blockingqueue,ConcurrentLinkedQueue,Executors
背景 通过做以下一个小的接口系统gate,了解一下mina和java并发包里的东西.A系统为javaweb项目,B为C语言项目,gate是本篇须要完毕的系统. 需求 1. A为集群系统,并发较高,会批 ...
- Python——Baseequestandler class (Interesting found in python‘s document)
class BaseHTTPRequestHandler(socketserver.StreamRequestHandler) HTTP request handler base class. | ...
- Excel中如何将时间戳转为时间?
Unix时间戳转换Excel时间? Excel中如何将时间戳转为时间? Excel默认不支持Unix格式时间戳,这在导入数据时十分不便.可以用以下公式将时间戳转换成Excel格式的时间: =(x+8* ...
- EXTJS4自学手册——组合图像
Ext.create('Ext.panel.Panel', { title: '组合图像', renderTo: 'ComplexDiagram', items: [{ xtype: 'button' ...
- Visual studio C++ MFC的库函数末尾添加“A”和“W”的意义
背景 在使用Visual studio C++ MFC库函数ModifyMenu(...)函数,在Visual studio自动代码补全里提示没有ModifyMenu(...)这个函数,倒是有Modi ...
- Python 的函数
Python 函数: 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你 ...
- docker发布spring cloud应用
原文地址:http://www.cnblogs.com/skyblog/p/5163691.html 本文涉及到的项目: cloud-simple-docker:一个简单的spring boot应用 ...