Node的核心模块


Node是在服务器端运行JS的环境

fs模块

fs模块是用来操作文件的模块, 提供了一系列的方法和属性,用来满足用户对文件的操作需求

下面是fs的用法案例

// 导入fs模块
const fs=require('fs');

// 调用 fs.readFile()读取文件的内容
fs.readFile('./成绩.txt','utf-8',function(err,dataStr){
    // 是否读取成功
    if(err){
        return console.log('读取文件失败'+err.message);
    }
    // 获取成功
    // console.log('读取成功'+dataStr);
    // 1.把成绩的数据按空格进行分割
    const arrOld=dataStr.split(' ')
    // console.log(arrOld);
    // 2.循环分割后的数组,对每一项数据,进行字符串的替换操作
    const arrNew=[];
    arrOld.forEach(item=>{
        arrNew.push(item.replace('=',':'))
    })
    // console.log(arrNew);
    // 3.把新数组中的每一项进行合并,得到一个新的字符串
    const newStr=arrNew.join('\r\n');
    // console.log(newStr);




    // 调用fs.writeFile()方法,把处理完毕的成绩,写入新文件中
    fs.writeFile('./成绩ok.txt',newStr,function(err){
        if(err){
            return console.log('写入失败'+err.message);
        }
        console.log('写入成功');
    });
})

最开始的文件如图
在这里插入图片描述
经过js处理之后,写入一个新文件
在这里插入图片描述
终端运行结果如图
在这里插入图片描述

path模块

path模块是用来处理路径的模块,提供了一系列方法和属性,用来满足用户对路径的处理需求。

该案例实现对一个HTML文件分离,把css和js单独分离出来

// 导入fs模块
const fs = require('fs');
// 导入path模块
const path = require('path');
// 定义正则表达式 匹配style标签,和script标签
const regStyle = /<style>[\s\S]*<\/style>/;

const regScript = /<script>[\s\S]*<\/script>/;

// 调用fs.readFile方法读取文件
fs.readFile(path.join(__dirname, '../clock.html'), 'utf-8', function (err, dataStr) {
    // 读取失败
    if (err) {
        return console.log('读取HTML文件失败' + err.message);
    }
    // 读取文件成功后,调用对应的三个方法,拆解出css,js,html文件
    resolveCSS(dataStr);
    resolveJS(dataStr);
    resolveHTML(dataStr);
})




// 定义处理css样式的方法
function resolveCSS(htmlStr){
    // 使用正则提取所需的内容
    const r1=regStyle.exec(htmlStr);
    // console.log(r1);
    // 字符串替换,将提取出来的样式字符串,进行replace操作
    const newCss=r1[0].replace('<style>','').replace('</style>','');
    // console.log(newCss);
    // 调用fs.writeFile()方法,将提取的文件写入到index.css文件中
    fs.writeFile(path.join(__dirname,'../css/index.css'),newCss,function(err){
        if(err) return console.log('写入css样式失败');
        console.log('写入样式文件成功');

    })
}

// 定义处理js的方法
function resolveJS(htmlStr){
    const r2=regScript.exec(htmlStr);
    const newJs=r2[0].replace('<script>','').replace('</script>','');
    fs.writeFile(path.join(__dirname,'../js/index.js'),newJs,function(err){
        if(err){
            return console.log('写入js失败');
        }
        console.log('写入js脚本成功');
    })
}


// 提取HTml
function resolveHTML(htmlStr){
    // 替换字符串,把内嵌的style和script标签换成link引入
    const newHTML=htmlStr.replace(regStyle,'<link rel="stylesheet" href="./css/index.css">')
            .replace(regScript,'<script src="./js/index.js"></script>');
    // 写入index.html文件
    fs.writeFile(path.join(__dirname,'../index.html'),newHTML,function(err){
        if(err){
            console.log('文件html写入失败');
        }
        console.log('写html 入成功');
    })
}

1.path.join()实现路径拼接
2.path.basename()获取对应文件的名称加后缀名
3.path.dirname()获取该文件夹的路径名
4.path.parse()将路径解析成对象
5.path.format()将对象整合成路径字符串
6.path.isAbsolute()是否是一个绝对路径

HTTP模块

用来创建web服务器的模块,通过HTTP模块提供的http.createServe()方法,就可以把一台普通电脑变为一台web服务器。

// 导入http模块
const http=require('http');
// 创建web服务器实例
const server=http.createServer();
// 为服务器实例绑定request事件,监听客户端的请求
server.on('request',(req,res)=>{
        console.log('someone visit our web server');
})
// 启动服务器
server.listen(8080,()=>{
    console.log('server running at http://127.0.0.1:8080');
})

终端运行结果:
在这里插入图片描述
当发起请求时在这里插入图片描述
会输出
在这里插入图片描述

URL模块

URL模块可以将获得的URL进行解析

const {URL}=require('url');//解构赋值,引入URL,并且URL是构造函数

let url='http://192.168.0.3:3000/soul/users/hb/sjz/xh.json?sex=1&age=[18,26]';
let myurl=new URL(url);
console.log(myurl); 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值