当前位置: 首页 > article >正文

Node.js学习记录(一)

目录

一、文件读取 readFile

二、写入文件 writeFile

三、动态路径 __dirname:表示当前文件所处的目录、path.join

四、获取路径文件名 path.basename

五、提取某文件中的css、JS、html

六、http

七、启动创建web服务器

服务器响应

八、将资源请求的 url 地址映射为文件的存放路径

九、模块

模块加载

模块作用域

模块加载机制

十、包

1、下载安装包

2、导入三方包并使用

3、包的管理

4、一次性安装所有包 

5、卸载包

6、开发节点 devDependencies

7、解决下载包慢问题

8、包的分类

9、包的规范

10、开发自己的包

模块化拆分

使用说明文档 README.md

11、发布包

①、注册账号

②、登录npm账号

③、发布

④、删除已发布的包


记录一下从B站学习node过程~

练习文件目录结构:myproject/node.js、1.txt、testnode空文件夹

1.txt文件内容:Hello node.js    node.js内容如下

一、文件读取 readFile

//node.js
//读取文件内容 fs.readFile()
const fs = require('fs')//导入fs模块
/*
  参数一:文件路径
  参数二:编码格式(可选参数)
  参数三:回调函数(失败、成功)如读取成功err返回null,读取失败err返回错误信息,data返回undefined
*/
fs.readFile('./1.txt', 'utf-8', (err, data) => {
  console.log('err',err)
  console.log('data',data)
})

文件读取成功和失败运行结果

二、写入文件 writeFile

//node.js
const fs = require('fs')//导入fs模块,调用fs.writeFile()写入文件
/*
  参数一:文件路径
  参数二:写入内容
  参数三:回调函数(失败、成功)如读取成功err返回null,读取失败err返回错误信息,data返回undefined
*/
fs.writeFile('./2.txt', 'HELLO NODE.JS', (err) => {
  console.log('写入文件 err',err)
})

运行成功和失败的结果,在myproject目录下创建了2.txt,并写入内容HELLO NODE.JS,如果在电脑不存在的a盘下创建则会失败并返回错误信息

三、动态路径 __dirname:表示当前文件所处的目录、path.join

防止路径拼写或过长不利维护等问题,用node提供的__dirname 动态拼写路径

const fs = require('fs')
const path = require('path')//导入path模块,调用path.join()拼接路径

//拼接:__dirname+'/1.txt'  或者使用模板字符串:`${__dirname}/1.txt`
//一般不推荐这样写,可以用node提供的path.join
//fs.readFile(`${__dirname}/1.txt`, 'utf-8', (err, data) => {

//推荐用这种方式
fs.readFile(path.join(__dirname,'/1.txt'), 'utf-8', (err, data) => {
  console.log('目录:', __dirname)
})

四、获取路径文件名 path.basename

const fs = require('fs')//导入fs模块,调用fs.readFile()读取文件
const path = require('path')//导入path模块,调用path.join()拼接路径

const forexample = 'E:/Desktop/前端/myproject/1.html'
console.log('获取文件名称:',path.basename(forexample))
console.log('获取不带后缀的文件名称:',path.basename(forexample,'.html'))

运行结果得到文件名称

五、提取某文件中的css、JS、html

新建一个与node.js同级的newHtml.html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no,minimum-scale=1,maximum-scale=1,viewport-fit=cover">
<title></title>
<link href="https://res.5i5j.com/wap/tools/common.js.css?t=1716465786" rel="stylesheet" type="text/css">
<link href="https://res.5i5j.com/wap/tools/jisuan.css?t=1716465786" rel="stylesheet" type="text/css">
<style>
*{font-size:16px;}
.test{height:100px;margin-bottom:10px;}
.test div{width:300px;height:60px;line-height: 60px;background: pink;}
</style>
</head>

<body class="test">
<div>这是一个新的 html</div>
<script>
function test(){
    console.log('hello newHtml')
}
</script>
</body>
</html>

const fs = require('fs')//导入fs模块,调用fs.readFile()读取文件
const path = require('path')//导入path模块,调用path.join()拼接路径

/*
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]
*/
/*正则定义匹配页面中的style和script标签*/
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/

fs.readFile(path.join(__dirname,'/input.html'),'utf-8',(err,data)=>{
  if(err) return console.log('读取html文件失败',err.message)
  // console.log('读取html文件成功',data)
  resolveCSS(data)

})
//获取CSS内容
function resolveCSS(htmlStr){
  const r1 = regStyle.exec(htmlStr)
  const newSty = r1[0].replace('<style>','').replace('</style>','')
  console.log('newSty==',newSty)
}
//获取JS内容
function resolveJS(htmlStr){
  const r1 = regScript .exec(htmlStr)
  const newJS = r1[0].replace('<script>','').replace('</script>','')
  console.log('newJS==',newJS)
}
//获取html
function resolveHtml(htmlStr){
   //直接获取html
  // const newHtml = htmlStr.replace(regStyle,'').replace(regScript,'')
  //获取html,把新创建的css和js引入
  const newHtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./output.css">').replace(regScript,'<script src="./output.js"></script>')
  console.log('newHtml==',newHtml)
  fs.writeFile(path.join(__dirname,'/testnode/output.html'),newHtml,(err)=>{
    if(err) return console.log('写入html文件失败',err.message)
  })
}

运行得到(注:先自己新建目录testnode空的文件夹)把得到的内容写入到新的文件中,如下

(r1 是一个数组)

六、http

域名 & 域名服务器(DNS,domain name server):IP 和 域名一 一对应,存这份关系的电脑就是域名服务器,

比如ping jd.com  得到的IP 和 jd.com 这就是一 一对应的关系

域名就是IP的别名,辅助人们记忆的;域名和IP的对应关系存放在域名服务器上(在浏览器中输入一个域名,会先经过域名服务器的地址转换,域名转换成IP,然后再进行访问)

端口号

每个端口号不能被多个web服务占用,实际应用中只有80端口可以省略不写

端口号就是一串数字,好比门牌号,可以在整个楼中准确的找到对应


http://www.kler.cn/news/295189.html

相关文章:

  • MySQL中常见的存储引擎有什么?
  • python操作数据对象方法和高阶函数
  • 19章 泛型(编程练习题)
  • windows C++-并行编程-将使用缩减变量的 OpenMP 循环转换为使用并发运行时
  • 经验笔记:负载均衡
  • Hive的优势与使用场景
  • WebTopo 组态软件+ARM 工业计算机:重塑污水处理
  • macos系统内置php文件列表 系统自带php卸载方法
  • 周报2024、9、8
  • 消息认证码(MAC)
  • HTTP与HTTPS:网络通信的安全之旅
  • 通信工程学习:什么是AB地址总线、DB数据总线、CD控制总线
  • 今日早报 每日精选15条新闻简报 每天一分钟 知晓天下事 9月8日,星期日
  • [动态规划] 删除并获得点数
  • el-table 封装表格(完整代码-实时更新)
  • 【技术调研】三维(0)-webGL、三维基础知识、前置知识、数学知识以及简单示例
  • 【Linux】服务器上在VSCode中运行JupyterNotebook
  • Exchange 服务器地址列表的配置方法与注意事项
  • 物联网之MQTT
  • 计算机视觉中,如何理解自适应和注意力机制的关系?
  • 云手机怎样简化海外社媒平台运营
  • 网关功能介绍
  • ffmpeg命令(详解)
  • 什么是GPT-3的自回归架构?为什么GPT-3无需梯度更新和微调
  • 数学基础 -- 统计学之零均值化
  • 小米Vela:端侧AI推理框架
  • 域名证书,泛域名证书,sni
  • 测试一些概念
  • Flutter集成Firebase框架
  • unity 实现吸血鬼幸存者的随机奖励