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之模块机制
> 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. target.click(function(){}); (2)target.bind("click",function( ...
- Spring集成Jersey开发(附demo)
下文将会初步介绍如何在Spring中集成Jersey,并附简单的demo 所依赖的技术版本: Jersey 1.8 Spring 3.0.5.RELEASE 1. 项目依赖 pom.xml定义(注意去 ...
- lua_pcall,lua_call 调用前后栈情况
lua_pcall和lua_call功能一样,只是lua_pcall提供了一个可以提供错误处理函数的功能 首先压入函数 ,再依次压入参数,现在你就可以调用lua_call了,函数调用后将参数,函数都弹 ...
- lucene: IO/FileNotFoundException:(Too many open files) 查询异常解决
http://stackoverflow.com/questions/6210348/too-many-open-files-error-on-lucene baidu zone - 为什么Luc ...
- list操作总结. dict操作及文件操作
1: 列表的操作 help(list) # 列表的帮助,列出所有列表的用法 type(name) # type判断数据类型是列表还是字典或者元组 isinstance("字符", ...
- C# 子类实例化过程
刚研究了一下C#子类实例化的过程. 首先我遇到了如下一个问题: 有类A,里面写了一个有参的构造函数,并没有提供默认的无参构造函数.现在类B继承了类A,没有写任何的构造函数. 这时如果想实例化类B就会产 ...
- Extjs中设置只读的样式问题
废话不多说,直接上代码: view.down('#imageFile').hide(); view.down('#save_button').hide(); view.show(); view.d ...
- Linux 脚本点滴知识积累
1.以openwrt中的/etc/hotplug.d/button/00-button为例 . /lib/functions.sh do_button () { local button ---- ...