nodejs fs模块、path模块、http 模块、npm 与 包
一、fs 文件模块
1. 引入 fs 模块
const fs = require('fs')
2. 文件写入 fs.writeFile(url, content, callback)
/**
* @param url 要写入的文件路径
* @param content 写入的文件内容。
* @param code 编码格式,非必填,默认utf8
* 注:①content会覆盖原本所有内容;②如果路径不存在,则会新建一个文件。
* @param callback 回调函数
*/
fs.writeFile("../testFile.txt", "测试写入文件", "utf8", function (err) {
console.log(err);
})
3. 文件读取 fs.readFile(url, [code,] callback)
/**
* @param url 要读取的文件路径
* @param code 编码格式,非必填,默认utf8
* @param callback 回调函数
* 第一个参数为报错信息,第二个参数为读取到的文件内容
*/
fs.readFile("../testFile.txt", "utf8", function (err, data) {
console.log(err, data)
})
4. 路径问题
fs.readFile("../testFile.txt", "utf8", function (err, data) {
console.log(err, data)
})
// 1. 不要使用相对路径
// 2. 不建议使用绝对路径
// 3. __dirname 表示当前文件所处的目录
fs.readFile(__dirname + "/testFile.txt", "utf8", function (err, data) {
console.log(err, data)
})
5. 其他方法
①追加文件内容 fs.appendFile()
/**
* @description 追加文件内容
* @param url 文件路径
* @param content 要追加的文件内容
* @param code 编码格式
* @param callback 回调函数
* 注:①如果路径不存在,则会创建该文件;②与writeFile方法不同,此方法不会覆盖掉原有内容。
*/
fs.appendFile('message.txt', 'data to append', "utf8", (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
②文件复制 fs.copyFile()
/**
* @description 文件复制
* @param src 要复制的源文件
* @param dest 目标文件
* @param mode 操作修饰符
* @param callback 回调函数
* 注:如果目标文件已存在,则会覆盖该文件
*/
fs.copyFile('message.txt', 'destination.txt', function (err) {
console.log(err);
});
③文件重命名 fs.rename()
/**
* @description 文件重命名
* @param oldFile 原文件
* @param newFile 新文件
* @param callback 回调函数
* 注:如果新文件已存在,则会覆盖该文件
*/
fs.rename('oldFile.txt', 'newFile.txt', (err) => {
if (err) throw err;
console.log('Rename complete!');
});
二、path 路径模块
1. 引入 path 模块
const path = require('path')
2. 路径片段拼接 path.join([…path])
const pathArr = path.join('/a','/b/c', '../s', './test')
console.log(pathArr); // \a\b\s\test
// 注:“../”会抵消掉一层路径
const pathArr = path.join(__dirname, './testFile.txt')
console.log(pathArr); // 当前文件所处目录/testFile.txt
3. 获取路径中的文件名 path.basename(path[, ext])
/**
* @param path 路径字符串
* @param ext 文件拓展名
* @returns 返回路径中的最后一个部分
*/
const fpath = "a/b/c/test.txt"
console.log(path.basename(fpath)); // test.txt
4. 获取路径中的文件拓展名 path.basename(path[, ext])
/**
* @param path 路径字符串
* @returns 返回拓展名
*/
const fpath = "a/b/c/test.txt"
console.log(path.extname(fpath)); // .txt
三、http 模块
1. 引入 http 模块
const http = require('http')
2. 基础使用
// 创建一个 Server s
const serve = http.createServer()
// 监听请求事件
serve.on("request", (req, res) => {
console.log("someone visit our serve", req, res);
// 请求对象 req
req.url;
req.method;
// 响应对象 res
res.setHeader("Content-Type", "text/html; charset=utf-8"); // 设置请求头,解决 end() 方法中返回中文时出现的乱码问题
// 请求结果
res.end("请求成功")
})
// 启动服务器
serve.listen(8088, function() {
console.log("serve is running at http://localhost:8088");
})
四、npm 与 包
1. 包的安装
// 例如时间格式化包 moment
npm install moment
npm i moment
2. 安装指定版本
npm install moment@2.22.2
npm i moment@2.22.2
// 安装时不需要卸载原先版本,会自动覆盖
3. 包的语义化版本规范
包的版本是以“点分十进制”形式进行定义的,总共有三位数字,例如:2.22.2
其中:
第一位数字代表 大版本
第二位数字代表 功能版本
第三位数字代表 bug修复版本
4. 快速创建 package.json
npm init -y
// 此命令只能在英文目录下运行,不要使用中文路径,不能出现空格
5. 核心依赖包 dependencies 与 开发依赖包 devDependencies
如果某些包只会在开发阶段用到,项目上线后不会用到,则建议把这些包安装到 devDependencies 中
npm i 包名 -D
// 等价于
npm i 包名 --save-dev
如实在无法区分,可在https://www.npmjs.com/ 中搜索对应的包,查看官方文档推荐。
5. 切换 npm 淘宝镜像源
// 查看当前镜像源地址
npm config get registry
// 设置镜像源地址
npm config set registry https://registry.npm.taobao.org
6. nrm
// 安装 nrm -g 代表全局安装
npm i nrm -g
// 查看所有可用的镜像源
nrm ls
// npm ---------- https://registry.npmjs.org/
// yarn --------- https://registry.yarnpkg.com/
// tencent ------ https://mirrors.cloud.tencent.com/npm/
// cnpm --------- https://r.cnpmjs.org/
// taobao ------- https://registry.npmmirror.com/
// npmMirror ---- https://skimdb.npmjs.com/registry/
// 使用 nrm 切换镜像
nrm use taobao
7. 发布自己的包到 npm 服务器
1. 准备好自己的包文件
2. 前往 npm 官网,注册 npm 账号
3. 在终端中输入 “npm login” 登录账号
4. 使用 “npm publish” 进行发布
5. 使用 “npm unpublish 包名 --force” 从 npm 中删除已发布的包
① npm unpublish 只能删除72小时内发布的包
② npm unpublish 删除的包,24小时内不允许再次发布
③ 不要发布没有意义的包
笔记来源:https://www.bilibili.com/video/BV1a34y167AZ/?p=33&spm_id_from=pageDriver&vd_source=8469bc86f044a56d2c8c7be5d1770d9a