常用的node核心模块:os、http、fs、path、url、querystring、crypto
Node.js 操作系统(os模块)
获取电脑系统相关信息
const os = require('os')
//返回编译Node.js二进制文件的操作系统CPU架构。
console.log( os.arch() )
//返回一个对象数组,其中包含关于每个逻辑CPU核心的信息。
console.log( os.cpus() )
Node.js 网络模块(http 模块)
可以创建一台服务器,来处理来自前端的请求
const http = require('http')
//创建一个服务器
http.createServer((req, resp) => {
// 如果返回的数据只需要一次性返回,可以直接在end里写返回的结果
// resp.end('hello')
resp.write('<h1>hello</h1>')
resp.end()
//监听8080端口
}).listen(8080, () => {
console.log('port 8080 is listening')
})
Node.js 文件系统(fs 模块)
模块中的方法均有异步和同步版本
readFile(读取文件)
const fs = require("fs");
// 异步读取
fs.readFile('data.txt', (err, data) => {
//抛出异常
if (err) throw err
// data是一个数据流
else console.log(data.toString())
})
// 同步读取
var data = fs.readFileSync('data.txt');
console.log("同步读取: " + data.toString());
writeFile(写入文件)
// 如果文件不存在则创建文件,如果文件已存在则覆盖已有内容
// 异步写入
fs.writeFile('data.txt', 'hello', (err) => {
// 抛出异常
if (err) throw err
// 写入成功
else console.log('done.')
})
// 同步写入
fs.writeFile('data.txt', 'hello')
appendFile(追加文件)
如果文件不存在,则创建文件,如果文件已存在则在文件末尾追加内容
fs.appendFile('data.txt', 'hello nz1908', err => {
if (err) throw err
else console.log('done.')
})
unlink(删除文件)
fs.unlink('data.txt', err => {
if (err) throw err
else console.log('done.')
})
// fs模块并没有直接提供修改文件的方法,可以先读出来,然后修改,重新writeFile覆盖
Node.js 目录路径(path模块)
处理文件和目录路径的实用程序
解析路径 path.parse()
// path
const path = require('path')
const str = 'C://nz/code/index.js'
// 解析成对象
console.log(path.parse(str))
//输出结果为
// root: 'C:/', //根,盘符
// dir: 'C://nz/code', //完整的路径
// base: 'index.js', //文件名称
// ext: '.js', //后缀名
// name: 'index' //文件名
序列化路径字符串 path.format()
const path = require('path')
const obj = {
root: 'D:/',
dir: 'D://nz1908/code',
base: 'index2.js',
ext: '.js',
name: 'index2'
}
// 序列化路径字符串
console.log(path.format(obj))
整合路径 path.join()
const path = require('path')
console.log(path.join('nz/code/node', '../index.js')
__dirname 全局魔术变量
无论在哪里都可以直接使用,代表当前路径
const path = require('path')
// console.log(__dirname)
Node.js 网络地址(url模块)
url解析和解析的实用程序
解析网络地址 path.parse()
const path = require('path')
const baidu = 'http://www.baidu.com/pic/icon/index.html?id=3&from=index#top'
//解析路径
console.log(url.parse(baidu))
//得到解析结果
// protocol: 'http:',
// slashes: true,
// auth: null,
// host: 'www.baidu.com',
// port: null,
// hostname: 'www.baidu.com',
// hash: '#top',
// search: '?id=3&from=index',
// query: 'id=3&from=index',
// pathname: '/pic/icon/index.html',
// path: '/pic/icon/index.html?id=3&from=index',
// href: 'http://www.baidu.com/pic/icon/index.html?id=3&from=index#top'
序列化地址字符串 url.format()
const path = require('path')
const obj = {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: '#top',
search: '?id=3&from=index',
query: 'id=3&from=index',
pathname: '/pic/icon/index.html',
path: '/pic/icon/index.html?id=3&from=index',
href: 'http://www.baidu.com/pic/icon/index.html?id=3&from=index#top'
}
console.log(url.format(obj))
Node.js 解析URL(querystring 模块)
解析和格式化URL查询字符串
解析网络地址 qs.parse()
const qs = require('querystring')
const str = 'id=3&from=index&type=1'
//解析str
console.log(qs.parse(str))
//得到对象
{ id: '3', from: 'index', type: '1' }
// 默认是按照&和=分割,也可以指定分隔符
const str = 'id,3;from,index;type,1'
console.log(qs.parse(str, ';', ','))
//也可以得到对象
{ id: '3', from: 'index', type: '1' }
序列化地址字符串 qs.stringify()
const qs = require('querystring')
const obj = {
name: 'zhangsan',
age: 18,
gender: 'male'
}
//将对象转化成url
console.log(qs.stringify(obj))
//得到:name=zhangsan&age=18&gender=male
//也可以指定分隔符
console.log(qs.stringify(obj, '%', '-'))
//得到:name-zhangsan%age-18%gender-male
转字符编码qs.encode()
const qs = require('querystring')
console.log(qs.escape('张三'))
//得到解码 %E5%BC%A0%E4%B8%89
解编码qs.unescape()
const qs = require('querystring')
console.log(qs.unescape('%E5%BC%A0%E4%B8%89'))
//得到解码 张三
Node.js 加密功能(crypto模块)
const crypto = require('crypto');
// 加密算法需要的密钥,不同的密钥加密同一个文本结果不一样的
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
//这里面输入想要加密的值
.update('I love cupcakes')
.digest('hex');
console.log(hash);
//得到加密的结果 32fbec1526d3210534cfa647d8b048cca3cd41e8c5152aa0ff1afc3711002bf0
// 这个算法只能加密不能解密,所以我们可以把两次的加密结果判断是否相等来决定原文是否相等