This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```JavaScript | |
| function ajax(url, fn) { | |
| //1.创建ajax对象.兼容IE6 | |
| var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); | |
| /** | |
| * 2.向服务器请求的方式 | |
| * GET 请求方式 | |
| * URL 请求地址 | |
| * true 同步异步的布尔值(true表示异步) | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Nodejs 新特性 async await 的使用以及使用 asyncawait 处理异步 | |
| # 目录 | |
| - 一、 Es 6 常见语法的使用 | |
| - 二、 Async、Await和Promise的使用 | |
| ## 一、 **ES6** 常见语法的使用 | |
| - 1.let const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 目录 | |
| - 1、fs.stat 检测是文件还是目录 | |
| - 2、fs.mkdir 创建目录 | |
| - 3、fs.writeFile 创建写入文件 | |
| - 4、fs.appendFile 追加文件和内容(文件存在的话只会追加内容) | |
| - 5、fs.readFile 读取文件 | |
| - 6、fs.readdir读取目录 | |
| - 7、fs.rename 功能:1重命名 2.移动文件 | |
| - 8、fs.rmdir 删除目录(只能删除空目录) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 一、包与NPM | |
| Nodejs 中除了它自己提供的核心模块外,我们可以自定义模块,也可以使用第三方的 | |
| 模块。Nodejs 中第三方模块由包组成,可以通过包来对一组具有相互依赖关系的模块进行统一管理。 | |
| ### 1.完全符合 CommonJs 规范的包目录一般包含如下这些文件。 | |
| - package.json :包描述文件。 | |
| - bin :用于存放可执行二进制文件的目录。 | |
| - lib :用于存放 JavaScript 代码的目录。 | |
| - doc :用于存放文档的目录。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| CommonJs 和 Nodejs 模块、自定义模块 | |
| ``` | |
| ## 目录 | |
| 一、什么是CommonJs? | |
| 二、Nodejs中的模块化 | |
| 三、npminit生成package.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## HTTP模块 | |
| ```JavaScript | |
| /** | |
| * Node.js http模块 | |
| */ | |
| const http = require('http'); //引入http模块 | |
| const port = 3000 | |
| const host = "localhost" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### 举例 | |
| ```JavaScript | |
| let userInfo={"name":"zhangsasn","age":20}; | |
| // console.log(`姓名:${userInfo.name},年龄:${userInfo.age}`); | |
| console.log("姓名: %s, 年龄: %s", userInfo.name, userInfo.age); | |
| ``` | |
| * `format` [<string>](http://nodejs.cn/s/9Tw2bK) 一个类似 `printf` 的格式字符串。 | |
| `util.format()` 方法返回一个格式化后的字符串,使用第一个参数作为一个类似 `printf` 的格式的字符串,该字符串可以包含零个或多个格式占位符。 每个占位符会被对应参数转换后的值所替换。 支持的占位符有: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```JavaScript | |
| var net = require('net'); | |
| // creates the server | |
| var server = net.createServer(); | |
| //emitted when server closes ...not emitted until all connections closes. | |
| server.on('close',function(){ | |
| console.log('Server closed !'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 网络是通信互联网的基础,Node.js 提供了 net、http、dgram 模块、分别用来实现 TCP、HTTP、UDP 的通信。上次的文章[《Node.js 构建 HTTP 服务器》](https://zc95.github.io/2018/03/19/nodejs-HTTP/)实现了 HTTP 的通信,这篇文章说一说 TCP 服务器和 TCP 客户端的构建。 | |
| [](#用Node-js创建TCP服务器 "用Node.js创建TCP服务器")用 Node.js 创建 TCP 服务器 | |
| ------------------------------------------------------------ | |
| ### [](#构建TCP服务器 "构建TCP服务器")构建 TCP 服务器 | |
| 为了使用 Node.js 创建 TCP 服务器,首先要使用 require(“net”) 来加载 net 模块,然后使用 net 模块的 createServer 方法就可以轻松地创建一个 TCP 服务器。 | |
| ```JavaScript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 方式一 | |
| ```JavaScript | |
| /** | |
| * 无法server.getConnections()统计 | |
| */ | |
| const PORT = 3000 | |
| const HOST = "localhost" | |
| /* 引入net模块 */ | |
| var net = require("net"); |