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)-如何动态显示当前运行行
在F11运行状态下,点击Online-Display Flow Control然后可以看到绿色的行就是当前正在运行行 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http:// ...
- Mysql 中文中繁杂的字 插入报错的 解决方案
首先 数据库默认编码选用 utf8 连接字符串也相应改成utf8,不能是gb2312
- yoman搭建你的react-webpack应用
还没有npm和node的要提前做好准备工作 做好这一切之后 我们安装yo,记住安装在全局变量中,我们需要用他的命令工具 npm install -g yo 接下来安装yo提供的react-webpac ...
- 使用 原生js 制作插件 (javaScript音乐播放器)
1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 在OpenErp的配置文件中为数据库密码加密
openerp连接数据库的用户名和密码可以命令行给出, 也可以设置在配置文件中, 如下例所示: db_user = openerp db_password = laoliu 因为它使用了明文的密码 ...
- C语言面向对象编程(五):单链表实现(转)
这里实现的单链表,可以存储任意数据类型,支持增.删.改.查找.插入等基本操作.(本文提供的是完整代码,可能有些长.) 下面是头文件: #ifndef SLIST_H #define SLIST_H # ...
- Android - 错误:Unable to instantiate application
错误:Unable to instantiate application 本文地址: http://blog.csdn.net/caroline_wendy 错误:java.lang.RuntimeE ...
- <转>C++位运算详解
原文转自:http://www.crazycpp.com/?p=82 前言 以前收藏过一篇讲C++位操作的文章,这次博客搬家,以前的数据都没有保留,整理谷歌网站管理后台的时候,发现不时的还有网友有在查 ...
- php中在局部作用域内访问全局变量
php中,由于作用域的限制,导致变量的访问限制: 1.局部作用域内不能访问全局变量 2.全局作用域内不能访问局部变量 对于第一种情况,如下代码将不能正常运行: <?php //局部作用域(函数内 ...
- 初学spring(二)
1.spring推荐使用接口编程,配合di可以达到层与层之间解耦