Node中的三大模块

一、全局模块(全局对象)

1、定义

何时何地都可以访问,不需要引用

2、全局对象和全局变量

什么是全局变量

  • 在最外层定义的变量;
  • 全局对象的属性;
  • 隐式定义的变量(未定义直接赋值的变量)

注意:

  • 在Node.js 中你不可能在最外层定义变量,因为所有用户代码都是属于当前模块的(模块作用域), 而模块本身不是最外层上下文
  • 永远使用var 定义变量以避免引入全局变量,因为全局变量会污染命名空间,提高代码的耦合风险。

3、process

process 是一个全局变量,即 global 对象的属性。
它用于描述当前Node.js 进程状态 的对象,提供了一个与操作系统的简单接口。

(1)process.argv 是命令行参数数组,第一个元素是 node,第二个元素是脚本文件名, 从第三个元素开始每个元素是一个运行参数。
(2)process.stdout是标准输出流,通常我们使用的 console.log() 向标准输出打印 字符,而 process.stdout.write() 函数提供了更底层的接口。
(3)process.stdin是标准输入流,初始时它是被暂停的,要想从标准输入读取数据, 你必须恢复流,并手动编写流的事件响应函数。
(4)process.nextTick(callback)的功能是为事件循环设置一项任务,Node.js 会在 下次事件循环调响应时调用 callback。
示例一:

console.log(process.argv);

在这里插入图片描述
示例二:

var num1 = parseInt(process.argv[2]);
var num2 = parseInt(process.argv[3]);

console.log(process.argv);
console.log(num1+num2);

在这里插入图片描述

4、console

(1)console.log 接受若干 个参数。如果只有一个参数,则输出这个参数的字符串形式。如果有多个参数,则 以类似于C 语言 printf() 命令的格式输出。
(2)console.error():与console.log() 用法相同,只是向标准错误流输出。
(3)console.trace():向标准错误流输出当前的调用栈。

console.log("我是console.log");
console.log("我是第%d个console.log",2);

在这里插入图片描述

二、核心模块

1、定义

需要 require(),但不需要单独下载

require()
1、如果有路径,就去路径里找
2、没有路径就去node_modules里找
3、以上两种都不符合就去node安装目录里面找

2、path

(1)使用 require()导入核心模块
(2)path.dirname() 接收一个参数,获取参数中文件的路径
(3)path.basename() 接收一个参数,获取参数中文件名
(4)path.extname() 接收一个参数,获取参数中文件的后缀名
(5)require.resolve()接收两个参数,查询某个模块文件的带有完整绝对路径的文件名

示例一:

var path = require('path');
console.log(path.dirname('/5html/nodeText/00.html'));
console.log(path.basename('/5html/nodeText/00.html'));
console.log(path.extname('/5html/nodeText/00.html'));

在这里插入图片描述
示例二:

var path = require('path');
console.log(path.resolve(__dirname,'00.html'));

在这里插入图片描述

3、fs

(1)使用 require()导入核心模块
(2)readFile()中接收两个参数。第一个参数是读取文件的路径,第二个参数是回调函数。回调函数中接收两个参数,第一个参数是 error对象,读取成功时error对象 为 null,失败时是错误信息;第二个参数是 data 数据,读取失败时·data 为 undifineddata 默认是二进制数据
(3)writeFile()中接收三个参数。第一个参数是要写入文件的路径,第二个参数是写入的内容,第三个参数是回调函数(同上)。
(4)writeFile()是对文件进行重写
(5)对文件内容进行追加则在writeFile()中增加一个参数:{flag:'a'}; 或者使用 appendFile()

示例一:

var fs = require('fs');
fs.readFile('./00.html',function(error,data){
  if(error) {
    console.log(error);
    console.log(data);
  }else {
    console.log(error);
    console.log(data);
    console.log(data.toString());
  }
})

读取文件成功:
在这里插入图片描述
读取文件失败:
在这里插入图片描述
示例二:

var fs = require('fs');
var msg = 'hello node';
fs.writeFile('./exam1.txt',msg,function(error) {
  if(error)
    console.log(error);
})

在这里插入图片描述
示例三:

var fs = require('fs');
var msg = 'hello node';
fs.writeFile('./exam1.txt',msg,{flag:'a'},function(error) {
  if(error)
    console.log(error);
})

在这里插入图片描述
示例四:

var fs = require('fs');
var msg = 'hello node';
fs.appendFile('./exam1.txt',msg,function(error) {
  if(error)
    console.log(error);
})

在这里插入图片描述

4、http

(1)在node中专门提供了一个核心模块:http 。http核心模块的作用就是创建编写服务器
(2)服务器作用:提供服务(对数据的服务);发送请求;接收请求;处理请求;发送响应(反馈)
(3)当客户端发出请求就会自动触发服务器的request请求事件,然后执行回调处理函数
(4)回调函数中有两个参数。依次为请求对象和响应对象。请求对象可以用来获取客户端的一些请求信息,例如请求路径。响应对象可以用来给客户端发送响应信息
(5)response对象中有一个方法:write,可以用来给客户端发送响应数据。write 可以使用多次,但是最后一定要使用end来结束响应,否则客户端会一直等待
(6)终端中打印的 “收到客户端请求,请求路径是/favicon.ico”浏览器默认图标

示例一:

//1、加载http核心模块
var http = require('http');

//2、使用http.createServer() 方法船舰一个Web服务器
//返回一个Server实例
var server = http.createServer();

//3、注册request请求事件
//当客户端发出请求就会自动触发服务器的request请求事件,然后执行回调处理函数
/*参数:request response
   请求对象可以用来获取客户端的一些请求信息,例如请求路径
   响应对象可以用来给客户端发送响应信息
*/
server.on('request',function(request,response)  {
    console.log("收到客户端请求,请求路径是" + request.url);  

    //response对象中有一个方法:write,可以用来给客户端发送响应数据
    //write 可以使用多次,但是最后一定要使用end来结束响应,否则客户端会一直等待
    response.write('hello node');
    response.end();
})

//4、绑定端口号,启动服务器
server.listen(8080,function() {
    console.log("服务器启动成功,请访问http://127.0.0.1:8080");
    
});

在这里插入图片描述
在这里插入图片描述
示例二:简写版

//1、加载http核心模块
var http = require('http');

http.createServer(function(request,response)  {
    console.log("收到客户端请求,请求路径是" + request.url);  
    response.write('hello node');
    response.end();
}).listen(8080;

console.log("服务器启动成功,请访问http://127.0.0.1:8080");
   

三、自定义模块

1、定义

require() 自己封装的模块

2、使用

  1. 在Node中,模块有:具名的核心模块、用户自己编写的文件模块(相对路径必须加 './ '),后缀名 '.js' 可以省略;'./' 不可以省略,否则报错
// ***00.js***
require('./01');
// ***01.js***
console.log("我是01");

在终端执行 node 00.js,终端打印 我是01

  1. 在Node中没有全局作用域,只有模块作用域。即外部访问不到内部,内部访问不到外部
// ***00.js***
var foo = '00';
function add(num1,num2) {
  return num1+num2;
}

require('./01');
console.log(foo);
// ***01.js***
console.log("我是01");
var foo = '01';
console.log(add(1,2));

在终端执行 node 00.jsconsole.log(foo); 中打印出 0001.jsconsole.log(add(1,2)); 语句报错

  1. require() 是一个方法,汽油两个作用。作用一是加载模块并执行里面的代码;作用二是拿到被加载文件模块导出的接口对象
  2. 模块默认是封闭的。那么进行自定义模块之间的通信就要用到require()方法加载自定i有模块,exports导出模块。在每个文件模块中都提供了一个对象:exports 对象,exports对象默认是一个空对象;还可以使用module

示例一:

// ***00.js***
var exp = require('./01');
console.log(exp.foo);
console.log(exp.add(2,3));
// ***01.js***
var foo = '01';
function add(num1,num2) {
    return num1*num2;
}
// 需要做的是把所有需要被外部访问的成员变量放在exports对象中
exports.foo = foo;
exports.add = function(num1,num2) {
    return num1+num2;
}

在终端执行 node 00.js
在这里插入图片描述
示例二:module 中为若干个变量

// ***00.js***
var exp = require('./01');
console.log(exp.foo);
console.log(exp.add(2,3));
// ***01.js***
var foo = '01';
function add(num1,num2) {
    return num1+num2;
}

module.exports = {
 foo,add
 }

在这里插入图片描述
示例三:module 中为一个对象

// ***00.js***
var exp = require('./03');

var p = new exp("zhangsan");
p.show();

// ***01.js***
module.exports = class {
    constructor(name) {
        this.name = name;
    }
    show() {
        console.log(this.name);
    }
}

在这里插入图片描述
示例四:module 中为一个函数

// ***00.js***
var exp = require('./03');
console.log(exp(1,2));
// ***01.js***
module.exports = function(num1,num2) {
    return num1+num2;
}

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章