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之模块机制
> 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 】AB题解
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- import * as obj from 'xx'
import * as obj from 'xx' 这种写法是把所有的输出包裹到obj对象里 例如: xx里中: export function hello(){ return '我是hello 内 ...
- C指针解析 ------ 运算符&和*
本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 & 是取地址运算符.* 叫做指针运算符或间接运算符.&a 的运算 ...
- Odoo8查询产品时提示"maximum recursion depth exceeded while calling a Python object"
今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品 ...
- libevent2源码分析之三:信号的初始化流程
libevent2对信号的响应也进行了封装,使之与socket操作一样对外提供统一的接口.这里的信号一般指linux的信号.由于信号与socket相关的编程接口有较大的不同,因此在内部实现也有一些区别 ...
- LeetCode 66 Plus One(加一)(vector)
翻译 给定一个以一系列数字表示的非负数.将其加一并转换成数字. 数字存储的最高位在列的最前面. 原文 Given a non-negative number represented as an arr ...
- IP报文格式及各字段意义
IP数据包由报头和数据两部分组成.报头的前一部分是固定长度,共20字节.在报头的固定部分的后面是可选部分——IP选项和填充域. 首部各字段的含义如下 1.版本 占4位,指IP协议的版本. 2 ...
- android studio - 隐藏编辑器标签块上面的索引
- C#动态调用WCF接口(2)
如何使用 1.第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用. 2.使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在I ...
- jquery的post()
jQuery ajax - post() 方法 jQuery Ajax 参考手册 实例 请求 test.php 网页,忽略返回值: $.post("test.php"); TIY ...