-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLearn-NodeJS.html
1 lines (1 loc) · 63.6 KB
/
Learn-NodeJS.html
1
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/favicon-32x32.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png"><link rel="mask-icon" href="/images/favicon-32x32.png" color="#222"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.css" integrity="sha256-no0c5ccDODBwp+9hSmV5VvPpKwHCpbVzXHexIkupM6U=" crossorigin="anonymous"><script src="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.js" integrity="sha256-a5YRB27CcBwBFcT5EF/f3E4vzIqyHrSR878nseNYw64=" crossorigin="anonymous"></script><script class="next-config" data-name="main" type="application/json">{"hostname":"www.wrysmile.cn","root":"/","images":"/images","scheme":"Gemini","version":"8.5.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":true,"color":"#222","save":"manual"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script><meta name="description" content="介绍 本文主要记录在学习尚硅谷的 Node.js 课程时的一些笔记 尚硅谷前端学科全套课程请点击这里进行下载,提取码:afyt"><meta property="og:type" content="article"><meta property="og:title" content="学习笔记:NodeJS(尚硅谷)"><meta property="og:url" content="https://www.wrysmile.cn/Learn-NodeJS.html"><meta property="og:site_name" content="Wrysmile 的博客"><meta property="og:description" content="介绍 本文主要记录在学习尚硅谷的 Node.js 课程时的一些笔记 尚硅谷前端学科全套课程请点击这里进行下载,提取码:afyt"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://z3.ax1x.com/2021/04/11/c0PjAg.png"><meta property="og:image" content="https://z3.ax1x.com/2021/04/11/c0FAMt.png"><meta property="og:image" content="https://z3.ax1x.com/2021/06/25/R3aDeI.png"><meta property="article:published_time" content="2021-04-11T02:30:09.000Z"><meta property="article:modified_time" content="2023-01-31T10:23:56.195Z"><meta property="article:author" content="Zhang Yangeng"><meta property="article:tag" content="前端"><meta property="article:tag" content="学习"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://z3.ax1x.com/2021/04/11/c0PjAg.png"><link rel="canonical" href="https://www.wrysmile.cn/Learn-NodeJS.html"><script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://www.wrysmile.cn/Learn-NodeJS.html","path":"Learn-NodeJS.html","title":"学习笔记:NodeJS(尚硅谷)"}</script><script class="next-config" data-name="calendar" type="application/json">""</script><title>学习笔记:NodeJS(尚硅谷) | Wrysmile 的博客</title><script>!function(e,t,o,c,i,a,n){e.DaoVoiceObject=i,e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=+new Date,a=t.createElement(o),n=t.getElementsByTagName(o)[0],a.async=1,a.src=c,a.charset="utf-8",n.parentNode.insertBefore(a,n)}(window,document,"script",("https:"==document.location.protocol?"https:":"http:")+"//widget.daovoice.io/widget/13e39436.js","daovoice"),daovoice("init",{app_id:"13e39436"}),daovoice("update")</script><noscript><link rel="stylesheet" href="/css/noscript.css"></noscript><link rel="alternate" href="/atom.xml" title="Wrysmile 的博客" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage" class="use-motion"><div class="headband"></div><main class="main"><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div></div><div class="site-meta"><a href="/" class="brand" rel="start"><i class="logo-line"></i><h1 class="site-title">Wrysmile 的博客</h1><i class="logo-line"></i></a><p class="site-subtitle" itemprop="description">欢迎来到我的个人小屋</p><img class="custom-logo-image" src="/images/favicon-32x32.png" alt="Wrysmile 的博客"></div><div class="site-nav-right"><div class="toggle popup-trigger"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close" role="button"><i class="fa fa-times-circle"></i></span></div><div class="search-result-container no-result"><div class="search-result-icon"><i class="fa fa-spinner fa-pulse fa-5x"></i></div></div></div></div></div><div class="toggle sidebar-toggle" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div><aside class="sidebar"><div class="sidebar-inner sidebar-nav-active sidebar-toc-active"><ul class="sidebar-nav"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="sidebar-panel-container"><div class="post-toc-wrap sidebar-panel"><div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">一、基础内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E8%A1%8C"><span class="nav-text">1.命令行</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4"><span class="nav-text">(1).命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%9B%AE%E5%BD%95"><span class="nav-text">(2).目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-text">(3).环境变量</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%AE%80%E4%BB%8B"><span class="nav-text">2.简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%9B%BE%E7%A4%BA"><span class="nav-text">3.图示</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%8E%86%E5%8F%B2"><span class="nav-text">4.历史</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%94%A8%E9%80%94"><span class="nav-text">5.用途</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%9B%B4%E6%96%B0"><span class="nav-text">6.更新</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-Windows%E4%B8%8B"><span class="nav-text">(1).Windows下</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-Linux%E4%B8%8B"><span class="nav-text">(2).Linux下</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E6%89%A7%E8%A1%8Cjs"><span class="nav-text">7.执行js</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">二、CommonJS规范</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A8%A1%E5%9D%97%E6%A0%87%E8%AF%86"><span class="nav-text">1.模块标识</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-text">2.模块化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%BC%95%E5%85%A5%E6%A8%A1%E5%9D%97"><span class="nav-text">(1).引入模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%9A%B4%E9%9C%B2%E5%8F%98%E9%87%8F%E6%88%96%E6%96%B9%E6%B3%95"><span class="nav-text">(2).暴露变量或方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E8%B0%83%E7%94%A8%E5%8F%98%E9%87%8F%E6%88%96%E6%96%B9%E6%B3%95"><span class="nav-text">(3).调用变量或方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%85%A8%E5%B1%80%E5%AF%B9%E8%B1%A1"><span class="nav-text">3.全局对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#exports%E4%B8%8Emodule-exports%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">exports与module.exports的区别</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%8C%85"><span class="nav-text">4.包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%8C%85%E7%BB%93%E6%9E%84"><span class="nav-text">(1).包结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%8C%85%E6%8F%8F%E8%BF%B0%E6%96%87%E4%BB%B6"><span class="nav-text">(2).包描述文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-NPM"><span class="nav-text">(3).NPM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-CNPM"><span class="nav-text">(4).CNPM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%AF%BB%E6%89%BE%E5%8C%85%E6%B5%81%E7%A8%8B"><span class="nav-text">(5).寻找包流程</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">三、Buffer(缓冲区)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-length%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">1.length的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%9B%B8%E5%85%B3%E6%96%B9%E6%B3%95"><span class="nav-text">2.相关方法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">四、fs(文件系统)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="nav-text">1.简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%96%87%E4%BB%B6%E5%86%99%E5%85%A5"><span class="nav-text">2.文件写入</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%99%AE%E9%80%9A%E5%86%99%E5%85%A5"><span class="nav-text">(1).普通写入</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#a-%E5%90%8C%E6%AD%A5%E4%BD%BF%E7%94%A8"><span class="nav-text">a).同步使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#b-%E5%BC%82%E6%AD%A5%E4%BD%BF%E7%94%A8"><span class="nav-text">b).异步使用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%AE%80%E5%8D%95%E5%86%99%E5%85%A5"><span class="nav-text">(2).简单写入</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#a-%E5%90%8C%E6%AD%A5%E4%BD%BF%E7%94%A8-2"><span class="nav-text">a).同步使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#b-%E5%BC%82%E6%AD%A5%E4%BD%BF%E7%94%A8-2"><span class="nav-text">b).异步使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#c-flag%E7%8A%B6%E6%80%81"><span class="nav-text">c).flag状态</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%B5%81%E5%BC%8F%E5%86%99%E5%85%A5"><span class="nav-text">(3).流式写入</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%96%87%E4%BB%B6%E8%AF%BB%E5%8F%96"><span class="nav-text">3.文件读取</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%99%AE%E9%80%9A%E8%AF%BB%E5%8F%96"><span class="nav-text">(1).普通读取</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%AE%80%E5%8D%95%E8%AF%BB%E5%8F%96"><span class="nav-text">(2).简单读取</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#a-%E5%90%8C%E6%AD%A5%E4%BD%BF%E7%94%A8-3"><span class="nav-text">a).同步使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#b-%E5%BC%82%E6%AD%A5%E4%BD%BF%E7%94%A8-3"><span class="nav-text">b).异步使用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%B5%81%E5%BC%8F%E8%AF%BB%E5%8F%96"><span class="nav-text">(3).流式读取</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#a-%E5%B8%B8%E8%A7%84%E8%AF%BB%E5%8F%96-%E5%86%99%E5%85%A5"><span class="nav-text">a).常规读取+写入</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#b-%E7%AE%80%E4%BE%BF%E8%AF%BB%E5%8F%96-%E5%86%99%E5%85%A5"><span class="nav-text">b).简便读取+写入</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%85%B6%E4%BB%96%E6%96%B9%E6%B3%95"><span class="nav-text">4.其他方法</span></a></li></ol></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="Zhang Yangeng" src="/images/avatar.jpg"><p class="site-author-name" itemprop="name">Zhang Yangeng</p><div class="site-description" itemprop="description">保持独立思考,不卑不亢不怂,长成自己想要的样子!</div></div><div class="site-state-wrap site-overview-item animated"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">69</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/"><span class="site-state-item-count">13</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">52</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author site-overview-item animated"><span class="links-of-author-item"><a href="https://github.com/zhangyangeng" title="GitHub → https://github.com/zhangyangeng" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://weibo.com/u/2294901673" title="weibo → https://weibo.com/u/2294901673" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-weibo fa-fw"></i>weibo</a></span></div><div class="cc-license site-overview-item animated" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="external nofollow noopener noreferrer" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/[email protected]/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a></div><div class="links-of-blogroll site-overview-item animated"><div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i> 推荐网站</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="http://topbook.cc/" title="http://topbook.cc/" rel="external nofollow noopener noreferrer" target="_blank">Topbook</a></li></ul></div></div></div></div></aside><div class="sidebar-dimmer"></div></header><div class="back-to-top" role="button" aria-label="返回顶部"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a><noscript><div class="noscript-warning">Theme NexT works best with JavaScript enabled</div></noscript><div class="main-inner post posts-expand"><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://www.wrysmile.cn/Learn-NodeJS.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/images/avatar.jpg"><meta itemprop="name" content="Zhang Yangeng"><meta itemprop="description" content="保持独立思考,不卑不亢不怂,长成自己想要的样子!"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Wrysmile 的博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">学习笔记:NodeJS(尚硅谷)</h1><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2021-04-11 10:30:09" itemprop="dateCreated datePublished" datetime="2021-04-11T10:30:09+08:00">2021-04-11</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2023-01-31 18:23:56" itemprop="dateModified" datetime="2023-01-31T18:23:56+08:00">2023-01-31</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">学习笔记</span></a> </span></span><span id="/Learn-NodeJS.html" class="post-meta-item leancloud_visitors" data-flag-title="学习笔记:NodeJS(尚硅谷)" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span></span></div><div class="post-meta"><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="far fa-file-word"></i> </span><span class="post-meta-item-text">本文字数:</span> <span>7.8k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="far fa-clock"></i> </span><span class="post-meta-item-text">阅读时长 ≈</span> <span>7 分钟</span></span></div></div></header><div class="post-body" itemprop="articleBody"><h1>介绍</h1><ul class="lvl-0"><li class="lvl-2">本文主要记录在学习尚硅谷的 Node.js 课程时的一些笔记</li><li class="lvl-2">尚硅谷前端学科全套课程请点击<a target="_blank" rel="external nofollow noopener noreferrer" href="https://pan.baidu.com/s/1BM_OKMXXAGxMNqaBN_7tRg">这里</a>进行下载,提取码:afyt</li></ul><span id="more"></span><h1>一、基础内容</h1><h2 id="1-命令行">1.命令行</h2><h3 id="1-命令">(1).命令</h3><ul class="lvl-0"><li class="lvl-2"><p><code>dir</code>:列出当前目录下的所有文件</p></li><li class="lvl-2"><p><code>cd 目录名</code>:进入指定目录</p></li><li class="lvl-2"><p><code>md 目录名</code>:新建文件夹</p></li><li class="lvl-2"><p><code>rd 目录名</code>:删除文件夹</p></li><li class="lvl-2"><p><code>a.txt</code>:直接打开当前目录下的文件</p></li></ul><h3 id="2-目录">(2).目录</h3><ul class="lvl-0"><li class="lvl-2"><p><code>.</code>:当前目录</p></li><li class="lvl-2"><p><code>..</code>:上一级目录</p></li></ul><h3 id="3-环境变量">(3).环境变量</h3><ul class="lvl-0"><li class="lvl-2"><p>当我们在命令行窗口打开一个文件或调用一个程序时</p></li><li class="lvl-2"><p>系统会首先在当前目录下寻找文件程序</p><ul class="lvl-2"><li class="lvl-4">如果找到了则直接打开</li><li class="lvl-4">如果没有找到则会依次到环境变量 path 的路径中寻找<ul class="lvl-4"><li class="lvl-6">如果找到了则会直接执行</li><li class="lvl-6">如果没有找到则报错</li></ul></li></ul></li><li class="lvl-2"><p>可以将一些常用的文件或程序的路径添加到 path 下,这样我们就可以在任意位置来访问这些文件了</p></li></ul><h2 id="2-简介">2.简介</h2><ul class="lvl-0"><li class="lvl-2"><p>中文网站:<a target="_blank" rel="external nofollow noopener noreferrer" href="http://nodejs.cn/">点击这里</a></p></li><li class="lvl-2"><p>Node.js 是一个能够在服务器端运行 JavaScript 的开放源代码、跨平台 JavaScript 运行环境</p></li><li class="lvl-2"><p>Node 采用 Google 开发的 V8 引擎运行 js 代码,使用<strong style="color:red">事件驱动、非阻塞和异步I/O模型</strong>等技术来提高性能,可优化应用程序的传输量和规模</p></li><li class="lvl-2"><p>Node 大部分基本模块都用 JavaScript 编写。在 Node 出现之前,JS 通常作为客户端程序设计语言使用,以 JS 写出的程序常在用户的浏览器上运行</p></li><li class="lvl-2"><p>Node 是事件驱动的,开发者可以在不使用线程的情况下开发出一个能够承载高并发的服务器。其他服务器端语言难以开发高并发应用,而且即使开发出来,性能也不尽人意</p></li><li class="lvl-2"><p>Node.js 允许通过 JS 和一系列模块来编写服务器端应用和网络相关的应用</p><ul class="lvl-2"><li class="lvl-4">核心模块包括文件系统 I/O、网络(HTTP、TCP、UDP、DNS、YLS/SSL等)、二进制数据流、加密算法、数据流等等</li><li class="lvl-4">Node 模块的 API 形式简单,降低了编程的复杂度</li></ul></li><li class="lvl-2"><p>Node.js 之父:<strong>瑞安·达尔(Ryan Dahl)</strong></p></li></ul><h2 id="3-图示">3.图示</h2><ul class="lvl-0"><li class="lvl-2"><p>一个网页需要经过如下几个步骤:</p><ul class="lvl-2"><li class="lvl-4">用户访问网页并发送请求给网站服务器</li><li class="lvl-4">服务器分出线程来处理该条请求,如果需要进行I/O操作时,会自动分出一条I/O线程</li><li class="lvl-4">处理完I/O操作后,服务器将网页响应返回给用户</li></ul></li><li class="lvl-2"><p>在如上的步骤中,第一和第三项可以做到优化,唯独无法优化的就是I/O请求</p></li><li class="lvl-2"><p>所以为了防止服务器多条进程造成阻塞现象,服务器端只能是单进程,这样才不会形成阻塞</p></li><li class="lvl-2"><p>示例图如下:<br><img src="https://z3.ax1x.com/2021/04/11/c0PjAg.png" alt="njs01.png"></p></li></ul><h2 id="4-历史">4.历史</h2><ul class="lvl-0"><li class="lvl-2"><p>Node.js 的历史发展如下图所示:<br><img src="https://z3.ax1x.com/2021/04/11/c0FAMt.png" alt="njs02.png"></p></li></ul><h2 id="5-用途">5.用途</h2><ul class="lvl-0"><li class="lvl-2"><p>Web 服务 API,比如 REST</p></li><li class="lvl-2"><p>实时多人游戏</p></li><li class="lvl-2"><p>后端的 Web 服务,例如跨域、服务器端的请求</p></li><li class="lvl-2"><p>基于 Web 的应用</p></li><li class="lvl-2"><p>多客户端的通信,如即时通信</p></li></ul><h2 id="6-更新">6.更新</h2><h3 id="1-Windows下">(1).Windows下</h3><ul class="lvl-0"><li class="lvl-2"><p>去官网下载最新版本的 <code>.msi</code> 文件并安装,就完成了 Node 版本更新</p></li><li class="lvl-2"><p>npm 是随 Node.js 一起发布的包管理工具,默认采用的并不一定是最新版本,如需升级在命令行使用以下命令:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm -g install npm ( 官方最新稳定版 )</span><br><span class="line">npm -g install [email protected] ( 自己需要的版本 )</span><br></pre></td></tr></table></figure><h3 id="2-Linux下">(2).Linux下</h3><ul class="lvl-0"><li class="lvl-2"><p>先查看本机 Node.js 版本:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node -v</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>清除 Node.js 的 cache</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo npm cache clean -f</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>安装 Node 版本管理工具,工具的名字有点奇葩,叫做 n</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo npm install -g n</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>安装最新版本的 Node.js</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo n stable</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>再次查看本机的 Node.js 版本</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node -v</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>更新 npm 到最新版</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo npm install -g npm</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>验证版本是否升级</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">node -v</span><br><span class="line">npm -v</span><br></pre></td></tr></table></figure><h2 id="7-执行js">7.执行js</h2><ul class="lvl-0"><li class="lvl-2"><p>在命令行中进入js文件所在目录</p></li><li class="lvl-2"><p>然后使用 <code>node hello.js</code> 命令来执行该js文件</p></li></ul><h1>二、CommonJS规范</h1><ul class="lvl-0"><li class="lvl-2"><p>目的:弥补当前 JavaScript 没有标准的缺陷</p></li><li class="lvl-2"><p>愿景:希望JS能够在任何地方运行</p></li><li class="lvl-2"><p>定义:模块引用、模块定义、模块标识</p></li></ul><h2 id="1-模块标识">1.模块标识</h2><ul class="lvl-0"><li class="lvl-2"><p>引入外部模块时,使用即为模块标识,可以通过模块标识来找到指定的模块</p></li><li class="lvl-2"><p>模块分为两大类:</p><ul class="lvl-2"><li class="lvl-4">核心模块:由Node引擎提供的模块,标识即为模块的名字</li><li class="lvl-4">文件模块:由用户自己创建的模块,标识即为文件的路径(绝对、相对路径)</li></ul></li></ul><h2 id="2-模块化">2.模块化</h2><ul class="lvl-0"><li class="lvl-2"><p>在Node中,一个JS文件就是一个模块</p></li><li class="lvl-2"><p>在Node中,每一个JS文件中的JS代码都是独立运行在一个函数中的,而不是全局作用域</p></li></ul><h3 id="1-引入模块">(1).引入模块</h3><ul class="lvl-0"><li class="lvl-2"><p>在Node中,通过 <code>require()</code> 函数来引入外部的模块</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">let md = require("./module");</span><br><span class="line">或</span><br><span class="line">let md = require("./module.js");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>路径如果使用相对路径,必须以 <code>./</code> 或 <code>../</code> 来开头</p></li><li class="lvl-2"><p>使用 <code>require()</code> 引入模块以后,该函数会返回一个对象,这个对象代表的是引入的模块</p></li></ul><h3 id="2-暴露变量或方法">(2).暴露变量或方法</h3><ul class="lvl-0"><li class="lvl-2"><p>在Node中,通过 <code>exports</code> 来向外部暴露变量和方法,只需要将需要暴露给外部的变量或方法设置为 <code>exports</code> 的属性即可</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">exports.x = "暴露的x"</span><br></pre></td></tr></table></figure><h3 id="3-调用变量或方法">(3).调用变量或方法</h3><ul class="lvl-0"><li class="lvl-2"><p>通过引入模块时定义的变量来调用暴露的变量或方法</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">md.x;</span><br></pre></td></tr></table></figure><h2 id="3-全局对象">3.全局对象</h2><ul class="lvl-0"><li class="lvl-2"><p>在 Node 中有一个全局对象 global,它的作用和网页中 Window 类似</p><ul class="lvl-2"><li class="lvl-4">在全局中创建的变量都会作为 global 的属性保存</li><li class="lvl-4">在全局中创建的函数都会作为 global 的方法保存</li></ul></li><li class="lvl-2"><p>当 Node 在执行模块中的代码时,它会在代码的外部添加如下代码:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function (exports, require, module, __filename, __dirname) {</span><br><span class="line"> 模块中的代码</span><br><span class="line"> console.log(arguments.callee + "");</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>实际上模块中的代码都是包装在一个函数中执行的并且在函数执行时,同时传递进了如下5个实参:</p><ul class="lvl-2"><li class="lvl-4">exports:用来将变量或函数暴露到外部</li><li class="lvl-4">require:函数,用来引入外部的模块</li><li class="lvl-4">module:代表的是当前模块本身,exports就是它的属性</li><li class="lvl-4">__filename:当前模块的完 整路径</li><li class="lvl-4">__dirname:当前模块所在文件夹的完整路径</li></ul></li></ul><h3 id="exports与module-exports的区别">exports与module.exports的区别</h3><ul class="lvl-0"><li class="lvl-2"><p>前者只能通过 <code>exports.xxx</code> 的方式来向外暴露内部变量,如:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">exports.xxx = xxx;</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>后者既可以通过 <code>module.exports.xxx</code> 的方式,也可以通过直接赋值来向外暴露内部变量,如:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">module.exports.xxx = xxx;</span><br><span class="line">module.exports = {};</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>赋值的区分方法:前者是直接修改了变量,而后者是修改了变量的属性(通过画引用数据类型的内存空间图来理解)</p></li></ul><h2 id="4-包">4.包</h2><ul class="lvl-0"><li class="lvl-2"><p>规范允许我们将一组相关的模块组合到一起,形成一组完整的工具</p></li><li class="lvl-2"><p><strong>包规范</strong>由<strong style="color:red">包结构</strong>和<strong style="color:red">包描述文件</strong>两个部分组成</p><ul class="lvl-2"><li class="lvl-4">包结构:用于组织包中的各种文件</li><li class="lvl-4">包描述文件:描述包的相关信息,以供外部读取分析</li></ul></li></ul><h3 id="1-包结构">(1).包结构</h3><ul class="lvl-0"><li class="lvl-2"><p>包实际上就是一个压缩文件,解压以后还原为目录,包含如下:</p><ul class="lvl-2"><li class="lvl-4"><code>package.json</code>:描述文件<strong style="color:red">(必须的)</strong></li><li class="lvl-4"><code>bin</code>:目录,存放可执行二进制文件</li><li class="lvl-4"><code>lib</code>:目录,存放js代码</li><li class="lvl-4"><code>doc</code>:目录,存放文档</li><li class="lvl-4"><code>test</code>:目录,存放单元测试文件</li></ul></li></ul><h3 id="2-包描述文件">(2).包描述文件</h3><ul class="lvl-0"><li class="lvl-2"><p>用于表达非代码相关的信息,是一个JSON格式的文件,位于包的根目录下,是包的重要组成部分</p></li><li class="lvl-2"><p><code>package.json</code> 主要含有:name、description、version、keywords、maintainers、contributors、bugs、licenses、repositories、dependencies 等</p></li><li class="lvl-2"><p><strong style="color:red">注意:任何JSON文件中都不可以写注释</strong></p></li></ul><h3 id="3-NPM">(3).NPM</h3><ul class="lvl-0"><li class="lvl-2"><p>npm 可实现第三方模块的发布、安装和依赖等</p></li><li class="lvl-2"><p>npm 命令:</p><ul class="lvl-2"><li class="lvl-4"><code>npm -v</code>:查看 npm 的版本</li><li class="lvl-4"><code>npm version</code>:查看所有模块的版本</li><li class="lvl-4"><code>npm init -y</code>:初始化 npm 且跳过手动设置(如需手动设置需去除-y)</li><li class="lvl-4"><code>npm search 包名</code>:搜索包</li><li class="lvl-4"><code>npm install/i 包名</code>:安装包</li><li class="lvl-4"><code>npm remove/r 包名</code>:删除包</li><li class="lvl-4"><code>npm install 包名 --save</code>:安装包并添加到依赖中</li><li class="lvl-4"><code>npm install</code>:下载当前项目所依赖的包</li><li class="lvl-4"><code>npm install 包名 -g</code>:全局安装包(一般都是一些工具)</li></ul></li><li class="lvl-2"><p>在安装包的时候,可能发现文件夹中并没有新增文件夹,可能是因为缺少了 <code>package.json</code> 文件,可通过初始化命令添加</p></li></ul><h3 id="4-CNPM">(4).CNPM</h3><ul class="lvl-0"><li class="lvl-2"><p>即为淘宝提供的 npm 镜像<a target="_blank" rel="external nofollow noopener noreferrer" href="http://npm.taobao.org/">网站</a></p></li><li class="lvl-2"><p>作用:解决 npm 下载缓慢问题</p></li></ul><h3 id="5-寻找包流程">(5).寻找包流程</h3><ul class="lvl-0"><li class="lvl-2"><p>node 在使用模块名字来引入模块时,会首先在当前目录的 <code>node_modules</code> 中寻找是否含有该模块</p><ul class="lvl-2"><li class="lvl-4">如果有则直接使用,如果没有则去上一层目录的 <code>node_modules</code> 中寻找</li><li class="lvl-4">如果有则直接使用,如果没有则再去上一层目录的 <code>node_modules</code> 中寻找,直到找到为止</li><li class="lvl-4">如果找到磁盘的根目录依然没有,则报错</li></ul></li></ul><h1>三、Buffer(缓冲区)</h1><ul class="lvl-0"><li class="lvl-2"><p>简介:从结构上看 Buffer 非常像一个数组,其元素为16进制的两位数,每个元素表示内存中的一个字节,因此可以直接通过 Buffer 来创建内存中的空间</p><ul class="lvl-2"><li class="lvl-4">每个元素的范围是从 00 - ff 的</li></ul></li><li class="lvl-2"><p>作用:专门用来<strong style="color:red">存储二进制数据</strong>(区别于数组)</p></li></ul><h2 id="1-length的区别">1.length的区别</h2><ul class="lvl-0"><li class="lvl-2"><p><code>buf.length</code>:计算的是占用内存的大小</p></li><li class="lvl-2"><p><code>str.length</code>:计算的是字符串的长度</p></li><li class="lvl-2"><p>区别:两个有时会相同有时不同,不同是因为一个汉字占内存空间中的3个字节,而长度只有1</p></li></ul><h2 id="2-相关方法">2.相关方法</h2><ul class="lvl-0"><li class="lvl-2"><p><code>Buffer.from("字符串")</code>:将一个字符串转换为 buffer</p></li><li class="lvl-2"><p><code>Buffer.alloc(size)</code>:创建一个指定大小的 buffer</p></li><li class="lvl-2"><p><code>Buffer.allocUnsafe(size)</code>:创建一个指定大小的 buffer,但是 buffer 中可能含有敏感数据(会残留当前内存区之前的数据)</p></li><li class="lvl-2"><p><code>buf[2]</code>:操作buf对象中的第3个元素,<strong style="color:red">若数字在控制台或页面中输出一定是十进制</strong></p><ul class="lvl-2"><li class="lvl-4">可使用 toString() 方法来转换显示的进制</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">buf2[1].toString(2); // 转换为二进制显示</span><br></pre></td></tr></table></figure></li><li class="lvl-2"><p><code>buf.toString()</code>:将缓冲区中的数据转换为字符串</p></li></ul><h1>四、fs(文件系统)</h1><h2 id="1-简介">1.简介</h2><ul class="lvl-0"><li class="lvl-2"><p>文件系统(File System)是通过 Node 中的 <code>fs</code> 模块来操作系统中的文件</p></li><li class="lvl-2"><p>该模块提供了一些标准文件访问 API 来打开、读取、写入文件,以及与其交互</p></li><li class="lvl-2"><p>该模块中的所有的操作都有<strong style="color:red">同步</strong>和<strong style="color:red">异步</strong>两种形式</p><ul class="lvl-2"><li class="lvl-4">同步:会阻塞程序的执行(带Sync)</li><li class="lvl-4">异步:不会阻塞程序的执行,通过回调函数将结果返回(不带Sync)</li></ul></li><li class="lvl-2"><p>该模块是核心模块,直接引入无需下载:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let fs = require("fs");</span><br></pre></td></tr></table></figure><h2 id="2-文件写入">2.文件写入</h2><h3 id="1-普通写入">(1).普通写入</h3><h4 id="a-同步使用">a).同步使用</h4><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.openSync()</code> 来<strong style="color:red">打开文件</strong>(该方法会返回一个文件的描述符作为结果,可以通过该描述符来对文件进行各种操作),参数为:</p><ul class="lvl-2"><li class="lvl-4">path:文件路径</li><li class="lvl-4">flags:操作的类型(w,r)</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let fd = fs.openSync("./file/test1.txt", "w");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.writeSync()</code> 来<strong style="color:red">写入文件</strong>,参数为:</p><ul class="lvl-2"><li class="lvl-4">fd:文件描述符</li><li class="lvl-4">string:要写入的内容</li><li class="lvl-4">position:写入的起始位置<strong style="color:red">(可选)</strong></li><li class="lvl-4">encoding:写入的编码,默认为 utf-8<strong style="color:red">(可选)</strong></li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fs.writeSync(fd, "测试文件的第一行文字");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.closeSync()</code> 来<strong style="color:red">关闭文件</strong>,参数为:</p><ul class="lvl-2"><li class="lvl-4">fd:文件描述符</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fs.closeSync(fd);</span><br></pre></td></tr></table></figure><h4 id="b-异步使用">b).异步使用</h4><ul class="lvl-0"><li class="lvl-2"><p>使用异步 API 时,只需要在同步的基础上增加回调函数即可,回调函数需要通过参数来返回相应的值,参数通常有:</p><ul class="lvl-2"><li class="lvl-4">err:错误对象,若没有错误即为 null</li><li class="lvl-4">fd:文件描述符</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 打开文件</span><br><span class="line">fs.open("./file/test2.txt", "w", function (err, fd){</span><br><span class="line"> if(!err){</span><br><span class="line"> // 写入内容</span><br><span class="line"> fs.write(fd, "异步操作的第一行文字", function (err){</span><br><span class="line"> if(!err){</span><br><span class="line"> console.log("成功添加内容");</span><br><span class="line"> }</span><br><span class="line"> // 关闭文件</span><br><span class="line"> fs.close(fd, function (err){</span><br><span class="line"> console.log(err);</span><br><span class="line"> })</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h3 id="2-简单写入">(2).简单写入</h3><h4 id="a-同步使用-2">a).同步使用</h4><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.writeFileSync()</code> 来写入,参数为:</p><ul class="lvl-2"><li class="lvl-4"><p>path:文件路径</p></li><li class="lvl-4"><p>data:要写入的内容</p></li><li class="lvl-4"><p>options:<strong style="color:red">可选</strong>,可以对写入进行一些设置</p></li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fs.writeFileSync("./file/test4.txt", "通过简单文件同步写入的内容");</span><br></pre></td></tr></table></figure><h4 id="b-异步使用-2">b).异步使用</h4><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.writeFile()</code> 来写入,参数比同步多一个回调函数</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">fs.writeFile("./file/test3.txt", "通过简单文件异步写入的内容", function (err){</span><br><span class="line"> console.log(err);</span><br><span class="line"> if(!err){</span><br><span class="line"> console.log("写入成功");</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="c-flag状态">c).flag状态</h4><ul class="lvl-0"><li class="lvl-2"><p>打开文件的状态如下图:<br><img src="https://z3.ax1x.com/2021/06/25/R3aDeI.png" alt="njs03.png"></p></li></ul><h3 id="3-流式写入">(3).流式写入</h3><blockquote><p>以上两种写入方法都不适合大文件的写入,性能较差,容易导致内存溢出,因此推荐使用流式写入方法</p></blockquote><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.createWriteStream()</code> 来创建一个可写流,参数为:</p><ul class="lvl-2"><li class="lvl-4">path:文件路径</li><li class="lvl-4">options:配置的参数,<strong style="color:red">可选</strong></li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let ws = fs.createWriteStream("./file/test5.txt");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>ws.write()</code> 来向文件中输入内容:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ws.write("第一次写入");</span><br><span class="line">ws.write("第二次写入");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>ws.close()/ws.end()</code> 来关闭该可写流(前者在低版本Node中会出现一些错误):</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ws.close();</span><br><span class="line">ws.end();</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>ws.once()</code> 可以为对象绑定一个一次性的事件来监听可写流的关闭与否:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ws.once("open", function (){</span><br><span class="line"> console.log("可写流打开了~~");</span><br><span class="line">})</span><br><span class="line">ws.once("close", function (){</span><br><span class="line"> console.log("可写流关闭了~~");</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h2 id="3-文件读取">3.文件读取</h2><h3 id="1-普通读取">(1).普通读取</h3><ul class="lvl-0"><li class="lvl-2"></li></ul><h3 id="2-简单读取">(2).简单读取</h3><h4 id="a-同步使用-3">a).同步使用</h4><ul class="lvl-0"><li class="lvl-2"></li></ul><h4 id="b-异步使用-3">b).异步使用</h4><ul class="lvl-0"><li class="lvl-2"><p>使用 <code>fs.readFile()</code> 来读取,参数比同步多一个回调函数</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">fs.readFile("./file/test1.txt", function (err, data){</span><br><span class="line"> if(!err){</span><br><span class="line"> console.log(data.toString());</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>若读取与写入同时使用时,可以达到复制的效果,如下:</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">fs.readFile("./file/1.jpg", function (err, data){</span><br><span class="line"> if(!err){</span><br><span class="line"> console.log(data);</span><br><span class="line"> fs.writeFile("./file/1_copy.jpg", data, function (err){</span><br><span class="line"> if (!err){</span><br><span class="line"> console.log("写入成功~~~");</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h3 id="3-流式读取">(3).流式读取</h3><h4 id="a-常规读取-写入">a).常规读取+写入</h4><ul class="lvl-0"><li class="lvl-2"><p>读取可读流中的数据,需要为可读流绑定一个 data 事件,事件绑定完毕会自动开始读取数据(读取到的数据都在回调函数的参数中)</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">// 创建一个可读流</span><br><span class="line">let rs = fs.createReadStream("./file/test1.txt");</span><br><span class="line">// 创建一个可写流</span><br><span class="line">let ws = fs.createWriteStream("./file/test1_copy.txt");</span><br><span class="line">// 监听是否开始关闭</span><br><span class="line">rs.once("open", function (){</span><br><span class="line"> console.log("可读流打开了");</span><br><span class="line">})</span><br><span class="line">rs.once("close", function (){</span><br><span class="line"> console.log("可读流关闭了");</span><br><span class="line"> ws.end();</span><br><span class="line">})</span><br><span class="line">// 读取可读流的数据</span><br><span class="line">rs.on("data", function (data){</span><br><span class="line"> console.log(data);</span><br><span class="line"> // 写入可写流中</span><br><span class="line"> ws.write(data);</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="b-简便读取-写入">b).简便读取+写入</h4><ul class="lvl-0"><li class="lvl-2"><p>无需绑定 data 事件,只需使用可写流的 <code>rs.pipe()</code> 方法即可将可读流中的内容直接输出到可写流中</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// 创建一个可写流</span><br><span class="line">let rs = fs.createReadStream("./file/那些花儿.mp3");</span><br><span class="line">// 创建一个可写流</span><br><span class="line">let ws = fs.createWriteStream("./file/那些花儿_copy.mp3");</span><br><span class="line">// 监听是否开始关闭</span><br><span class="line">rs.once("open", function (){</span><br><span class="line"> console.log("可读流打开了");</span><br><span class="line">})</span><br><span class="line">rs.once("close", function (){</span><br><span class="line"> console.log("可读流关闭了");</span><br><span class="line">})</span><br><span class="line">rs.pipe(ws);</span><br></pre></td></tr></table></figure><h2 id="4-其他方法">4.其他方法</h2><ul class="lvl-0"><li class="lvl-2"><p><code>fs.existsSync(path)</code>:检查一个文件是否存在</p></li><li class="lvl-2"><p><code>fs.stat(path,callback)/fs.statSync(path)</code>:获取文件的状态</p></li><li class="lvl-2"><p><code>fs.unlink(path,callback)/fs.unlinkSync(path)</code>:删除文件</p></li><li class="lvl-2"><p><code>fs.readdir(path[,options],callback)/fs.readdirSync(path[,options])</code>:读取一个目录的目录结构</p></li><li class="lvl-2"><p><code>fs.truncate(path,len,callback) / fs.truncateSync(path,len)</code>:截断文件,将文件修改为指定的大小</p></li><li class="lvl-2"><p><code>fs.mkdir(path,[options],callback) / fs.mkdirSync(path,[options])</code>:创建一个目录</p></li><li class="lvl-2"><p><code>fs.rmdir(path,callback) / fs.rmdirSync(path)</code>:删除一个目录</p></li><li class="lvl-2"><p><code>fs.rename(oldPath,newPath,callback) / fs.renameSync(oldPath,newPath)</code>:对文件进行重命名,同时可以实现移动的效果</p></li><li class="lvl-2"><p><code>fs.watchFire(filename[,options],listener)</code>:监视文件的修改</p></li></ul></div><footer class="post-footer"><div class="my_post_copyright"><script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script><script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script><script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script><p><span>本文标题:</span><a href="/Learn-NodeJS.html">学习笔记:NodeJS(尚硅谷)</a></p><p><span>文章作者:</span><a href="/" title="访问 Zhang Yangeng 的个人博客">Zhang Yangeng</a></p><p><span>发布时间:</span>2021年04月11日 - 10:04</p><p><span>最后更新:</span>2023年01月31日 - 18:01</p><p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="external nofollow noopener noreferrer" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a></p><p><span>注意事项:</span>转载请保留原文链接及作者,请尊重作者的劳动成果</p><p><span>原始链接:</span><a href="/Learn-NodeJS.html" title="学习笔记:NodeJS(尚硅谷)">https://www.wrysmile.cn/Learn-NodeJS.html</a> <span class="copy-path" title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://www.wrysmile.cn/Learn-NodeJS.html" aria-label="复制成功!"></i></span></p></div><script>var clipboard=new Clipboard(".fa-clipboard");$(".fa-clipboard").click(function(){clipboard.on("success",function(){swal({title:"",text:"复制成功",icon:"success",showConfirmButton:!0})})})</script><div class="reward-container"><div>坚持原创技术分享,您的支持将是对我最大的鼓励!</div><button onclick='document.querySelector(".post-reward").classList.toggle("active")'>赞赏</button><div class="post-reward"><div><img src="https://s2.ax1x.com/2019/01/28/kKrX7T.png" alt="Zhang Yangeng 微信"> <span>微信</span></div><div><img src="https://i.loli.net/2019/01/25/5c4ac75f2e5b7.jpg" alt="Zhang Yangeng 支付宝"> <span>支付宝</span></div></div></div><div class="post-tags"><a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 前端</a> <a href="/tags/%E5%AD%A6%E4%B9%A0/" rel="tag"><i class="fa fa-tag"></i> 学习</a></div><div class="post-nav"><div class="post-nav-item"><a href="/Learn-jQuery.html" rel="prev" title="学习笔记:jQuery(尚硅谷)"><i class="fa fa-chevron-left"></i> 学习笔记:jQuery(尚硅谷)</a></div><div class="post-nav-item"><a href="/Learn-MongoDB.html" rel="next" title="学习笔记:MongoDB(尚硅谷)">学习笔记:MongoDB(尚硅谷) <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div><div class="comments" id="lv-container" data-id="city" data-uid="MTAyMC80MjQ5Ni8xOTA0Mw=="></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">© 2019 – <span itemprop="copyrightYear">2023</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">Wrysmile</span></div><div class="wordcount"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-chart-line"></i> </span><span>站点总字数:</span> <span title="站点总字数">413k</span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span>站点阅读时长 ≈</span> <span title="站点阅读时长">6:15</span></span></div><div class="busuanzi-count"><span class="post-meta-item" id="busuanzi_container_site_uv"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-item" id="busuanzi_container_site_pv"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div><script color="0,0,255" opacity="0.5" zindex="-1" count="99" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script><script type="text/javascript">!function(e,r){function t(){for(var e=0;e<n.length;e++)n[e].alpha<=0?(r.body.removeChild(n[e].el),n.splice(e,1)):(n[e].y--,n[e].scale+=.004,n[e].alpha-=.013,n[e].el.style.cssText="left:"+n[e].x+"px;top:"+n[e].y+"px;opacity:"+n[e].alpha+";transform:scale("+n[e].scale+","+n[e].scale+") rotate(45deg);background:"+n[e].color+";z-index:99999");requestAnimationFrame(t)}var o,n=[];e.requestAnimationFrame=e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)},function(t){var a=r.createElement("style");a.type="text/css";try{a.appendChild(r.createTextNode(t))}catch(e){a.styleSheet.cssText=t}r.getElementsByTagName("head")[0].appendChild(a)}(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o="function"==typeof e.onclick&&e.onclick,e.onclick=function(e){var t,a;o&&o(),t=e,(a=r.createElement("div")).className="heart",n.push({el:a,x:t.clientX-5,y:t.clientY-5,scale:1,alpha:1,color:"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}),r.body.appendChild(a)},t()}(window,document)</script><script type="text/javascript">var titleTime,OriginTitle=document.title;document.addEventListener("visibilitychange",function(){document.hidden?($('[rel="icon"]').attr("href","/img/TEP.ico"),document.title="╭(°A°`)╮ 页面崩溃啦 ~",clearTimeout(titleTime)):($('[rel="icon"]').attr("href","/favicon.ico"),document.title="(ฅ>ω<*ฅ) 噫又好了~"+OriginTitle,titleTime=setTimeout(function(){document.title=OriginTitle},2e3))})</script></div></footer><script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script><script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/third-party/search/local-search.js"></script><script class="next-config" data-name="nprogress" type="application/json">{"enable":true,"spinner":false}</script><script src="/js/third-party/nprogress.js"></script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"mzhbgeWR9BA3SvqtvaAJulC1-gzGzoHsz","app_key":"xcGndtx2gmyop5yrT6euUk9G","server_url":null,"security":false}</script><script src="/js/third-party/statistics/lean-analytics.js"></script><script src="/js/third-party/comments/livere.js"></script></body></html>