前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >三、nodejs模块使用和其他

三、nodejs模块使用和其他

作者头像
Dreamy.TZK
发布2020-07-03 10:12:41
6910
发布2020-07-03 10:12:41
举报

内置模块的基本使用(删除文件)

const fs = require("fs");

/**
 * 删除文件
 * @param {string} "./temp/test.txt" 被删除文件的路径
 * @param {function} (err) 回调函数,将错误信息传入其中
 */
fs.unlink("./temp/test.txt", (err) => {
  if (err) throw err;
  console.log("已成功删除 test");
});

fs模块读文件

const fs = require("fs");

/**
 * 读取文件信息
 * @date 2020-07-01
 * @param {sting} "./temp/t.txt" 文件路径
 * @param {sting} "utf-8" 指定字符编码
 * @param {function} (err,data) 回调函数传入错误信息与读到的数据
 */
fs.readFile("./temp/t.txt", "utf-8", (err, data) => {
  if (err) throw err;
  console.log(data);
});

fs模块写文件

如果没有文件夹那么会抛出错误,但是没有文件则会自动创建文件。如果已经存在,则会覆盖。

const fs = require("fs");

const data1 = `
这是一个测试
`;

/**
 * 写文件
 * @param {any} "./temp/1.txt" 待写入文件的路径
 * @param {any} data 待写入的数据
 * @param {function} err 回调函数传入错误类型
 */
fs.writeFile("./temp/1.txt", data1, (err) => {
  if (err) throw err;
  console.log("文件已保存");
});

同步异步

  • 同步 所谓同步也就是按顺序执行
  • 异步 同时执行,谁先做完谁输出。不会造成阻塞。
image-20200701195212126
image-20200701195212126
  1. 面试题-1 var t = true; windows.setTimeout(function () { t = false; }, 1000); while (t) {} console.log("end");以上代码会无限循环,因为定时器是异步执行。则先进入循环,一旦进入,那么就会发生无限循环。
  2. 面试题-2 var t = true; while (t) { windows.setTimeout(function () { t = false; }, 1000); } console.log("end");以上代码还是会死循环。计时器虽然已经声明了,但计时器的回调函数无法被执行。

相对路径的问题

路径问题

NodeJs中的相对路径是相对于执行js文件的终端工具路径而言。

const fs = require("fs");

fs.readFile("./temp/1.txt", "utf-8", (err, data) => {
  if (err == null) {
    console.log(data);
  } else {
    console.log(err);
  }
});
image-20200702082329200
image-20200702082329200

和路径相关的变量

  • 获取当前文件所在目录 __dirname
  • 获取当前文件的绝对路径 __filename

以上两个变量不需要定义即可使用。

console.log(__dirname);
// d:\coder\studyCode\project\2020\07\02
console.log(__filename);
// d:\coder\studyCode\project\2020\07\02\06-路径相关的变量.js
image-20200702082907423
image-20200702082907423

因此要读取的文件可以使用此变量进行拼接。

const fs = require("fs");
console.log(__dirname);
console.log(__filename);
fs.readFile(__dirname + "/temp/1.txt", "utf-8", (err, data) => {
  if (err == null) {
    console.log(data);
  } else {
    console.log(err);
  }
});

path模块

为了避免出现少写斜杠(\)的错误出现。

  1. join路径拼接方法 path.join()方法使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径。 const path = require("path"); const fs = require("fs"); const fullPath = path.join(__dirname, "temp", "1.txt"); console.log(fullPath); fs.readFile(fullPath, "utf-8", (err, data) => { if (err == null) { console.log(data); } else { console.log(err); } });
image-20200702084332456
image-20200702084332456

http模块

创建一个简单的本地服务器

const http = require("http");

// 创建服务器
// 返回值代表服务器
const server = http.createServer((request, response) => {
  response.end("Hello World");
});

// 开启服务器
server.listen(8080, () => {
  console.log("服务器已经开启:8080");
});
image-20200702085231673
image-20200702085231673

中文乱码问题

解决中文乱码须在响应头设置Content-Type

const http = require("http");

// 创建服务器
// 返回值代表服务器
const server = http.createServer((request, response) => {
    // 设置响应头,防止中文乱码
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    response.end("你好,世界");
});

// 开启服务器
server.listen(8080, () => {
    console.log("服务器已经开启:8080");
});

nodemon工具

此工具作用为自动监视文件的修改,自动重新运行。

  • 安装 npm install nodemon -g
  • 使用 nodemon 文件名
115c2d23-7812-4cf5-94c1-d763c77f2b5c
115c2d23-7812-4cf5-94c1-d763c77f2b5c
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 内置模块的基本使用(删除文件)
  • fs模块读文件
  • fs模块写文件
  • 同步异步
  • 相对路径的问题
    • 路径问题
      • 和路径相关的变量
      • path模块
      • http模块
      • nodemon工具
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档