From 3598af0e1654c72068d27ec0178d945db785e119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= <3041920171@qq.com> Date: Mon, 13 Feb 2023 08:02:11 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E9=99=88=E5=A9=B7?= =?UTF-8?q?=E5=A9=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\351\231\210\345\251\267\345\251\267/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\351\231\210\345\251\267\345\251\267/.keep" diff --git "a/\351\231\210\345\251\267\345\251\267/.keep" "b/\351\231\210\345\251\267\345\251\267/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 555a953aba1d55ba9a995221ab8502fa619088db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= <3041920171.com> Date: Mon, 13 Feb 2023 17:20:23 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\351\231\210\345\251\267\345\251\267/.keep" | 0 ...-\347\254\254\344\270\200\346\254\241\350\257\276.md" | 9 +++++++++ 2 files changed, 9 insertions(+) delete mode 100644 "\351\231\210\345\251\267\345\251\267/.keep" create mode 100644 "\351\231\210\345\251\267\345\251\267/20230213--\347\254\254\344\270\200\346\254\241\350\257\276.md" diff --git "a/\351\231\210\345\251\267\345\251\267/.keep" "b/\351\231\210\345\251\267\345\251\267/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\351\231\210\345\251\267\345\251\267/20230213--\347\254\254\344\270\200\346\254\241\350\257\276.md" "b/\351\231\210\345\251\267\345\251\267/20230213--\347\254\254\344\270\200\346\254\241\350\257\276.md" new file mode 100644 index 0000000..233af11 --- /dev/null +++ "b/\351\231\210\345\251\267\345\251\267/20230213--\347\254\254\344\270\200\346\254\241\350\257\276.md" @@ -0,0 +1,9 @@ +2023年2月13日 + +我开学了!!! + + +Node.js + + + -- Gitee From 7b1373070f61cec9dafcd50616ef2e4f1cef9fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= <3041920171@qq.com> Date: Thu, 16 Feb 2023 17:12:25 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\344\270\216\346\250\241\345\235\227.md" | 66 +++++++++++ ...72\346\234\254\346\250\241\345\235\227.md" | 103 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 "\351\231\210\345\251\267\345\251\267/20230215--\350\260\203\350\257\225\344\270\216\346\250\241\345\235\227.md" create mode 100644 "\351\231\210\345\251\267\345\251\267/20230216--\345\237\272\346\234\254\346\250\241\345\235\227.md" diff --git "a/\351\231\210\345\251\267\345\251\267/20230215--\350\260\203\350\257\225\344\270\216\346\250\241\345\235\227.md" "b/\351\231\210\345\251\267\345\251\267/20230215--\350\260\203\350\257\225\344\270\216\346\250\241\345\235\227.md" new file mode 100644 index 0000000..d2b92ed --- /dev/null +++ "b/\351\231\210\345\251\267\345\251\267/20230215--\350\260\203\350\257\225\344\270\216\346\250\241\345\235\227.md" @@ -0,0 +1,66 @@ +## 相对路径 + +指两个文件存在引用关系的时候的一种路径表达采用的简洁方式 + +## 绝对路径 + +指两个文件存在引用关系的时候的一种路径表达采用完整的路径方式 + +## 网络路径 + +引用非本地路径的表达方式 + + + +# 运行和调试JavaScript +在VS Code中,我们可以非常方便地运行JavaScript文件。 + +VS Code以文件夹作为工程目录(Workspace Dir),所有的JavaScript文件都存放在该目录下。此外,VS Code在工程目录下还需要一个.vscode的配置目录,里面存放里VS Code需要的配置文件。 + +假设我们在C:\Work\目录下创建了一个hello目录作为工程目录,并编写了一个hello.js文件,则该工程目录的结构如下: +``` +hello/ <-- workspace dir +| ++- hello.js <-- JavaScript file +| ++- .vscode/ <-- VS Code config + | + +- launch.json <-- VS Code config file for JavaScript + ``` + +可以用VS Code快速创建launch.json,然后修改如下: +```js +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Run hello.js", + "type": "node", + "request": "launch", + "program": "${workspaceRoot}/hello.js", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceRoot}", + "preLaunchTask": null, + "runtimeExecutable": null, + "runtimeArgs": [ + "--nolazy" + ], + "env": { + "NODE_ENV": "development" + }, + "externalConsole": false, + "sourceMaps": false, + "outDir": null + } + ] +} +``` +有了配置文件,即可使用VS Code调试JavaScript。 + + +# 模块 + +在node环境中,一个.js文件就称之为一个模块(module) +编写一个hello.js文件,这个hello.js文件就是一个模块,模块的名字就是文件名(去掉.js后缀)所以hello.js文件就是名为hello的模块。 + diff --git "a/\351\231\210\345\251\267\345\251\267/20230216--\345\237\272\346\234\254\346\250\241\345\235\227.md" "b/\351\231\210\345\251\267\345\251\267/20230216--\345\237\272\346\234\254\346\250\241\345\235\227.md" new file mode 100644 index 0000000..a3d9832 --- /dev/null +++ "b/\351\231\210\345\251\267\345\251\267/20230216--\345\237\272\346\234\254\346\250\241\345\235\227.md" @@ -0,0 +1,103 @@ + +# 模块 +## global +在前面的JavaScript课程中,我们已经知道,JavaScript有且仅有一个全局对象,在浏览器中,叫window对象。而在Node.js环境中,也有唯一的全局对象,但不叫window,而叫global,这个对象的属性和方法也和浏览器环境的window不同。进入Node.js交互环境,可以直接输入: + + global.console + +```js +Console { + log: [Function: bound ], + info: [Function: bound ], + warn: [Function: bound ], + error: [Function: bound ], + dir: [Function: bound ], + time: [Function: bound ], + timeEnd: [Function: bound ], + trace: [Function: bound trace], + assert: [Function: bound ], + Console: [Function: Console] } + ``` +## process +process也是Node.js提供的一个对象,它代表当前Node.js进程。通过process对象可以拿到许多有用信息: +```js +> process === global.process; +true +> process.version; +'v5.2.0' +> process.platform; +'darwin' +> process.arch; +'x64' +> process.cwd(); //返回当前工作目录 +'/Users/michael' +> process.chdir('/private/tmp'); // 切换当前工作目录 +undefined +> process.cwd(); +'/private/tmp'\ +``` +JavaScript程序是由事件驱动执行的单线程模型,Node.js也不例外。Node.js不断执行响应事件的JavaScript函数,直到没有任何响应事件的函数可以执行时,Node.js就退出了。 + +如果我们想要在下一次事件响应中执行代码,可以调用process.nextTick(): + +// test.js +```js +// process.nextTick()将在下一轮事件循环中调用: +process.nextTick(function () { + console.log('nextTick callback!'); +}); +console.log('nextTick was set!'); +``` +用Node执行上面的代码node test.js,你会看到,打印输出是: +```js +nextTick was set! +nextTick callback! +``` +这说明传入process.nextTick()的函数不是立刻执行,而是要等到下一次事件循环。 + +Node.js进程本身的事件就由process对象来处理。如果我们响应exit事件,就可以在程序即将退出时执行某个回调函数: + +// 程序即将退出时的回调函数: +```js +process.on('exit', function (code) { + console.log('about to exit with code: ' + code); +}); +``` + + +# 文件系统模块 + +# fs +Node.js内置的fs模块就是文件系统模块,负责读写文件。 + +和所有其它JavaScript模块不同的是,fs模块同时提供了异步和同步的方法。 + +## 异步读文件 +按照JavaScript的标准,异步读取一个文本文件的代码如下: + +```js +'use strict'; + +var fs = require('fs'); + +fs.readFile('sample.txt', 'utf-8', function (err, data) { + if (err) { + console.log(err); + } else { + console.log(data); + } +}); +``` + +## 同步读文件 + +用fs模块同步读取一个文本文件的代码如下: + +```js +'use strict'; + +var fs = require('fs'); + +var data = fs.readFileSync('sample.txt', 'utf-8'); +console.log(data); +``` \ No newline at end of file -- Gitee From 1c008cad63c7c3765ab083f781aa2d6cc492303c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= <3041920171@qq.com> Date: Fri, 17 Feb 2023 11:45:09 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=99=88=E5=A9=B7=E5=A9=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20230217--http\346\250\241\345\235\227.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "\351\231\210\345\251\267\345\251\267/20230217--http\346\250\241\345\235\227.md" diff --git "a/\351\231\210\345\251\267\345\251\267/20230217--http\346\250\241\345\235\227.md" "b/\351\231\210\345\251\267\345\251\267/20230217--http\346\250\241\345\235\227.md" new file mode 100644 index 0000000..6074ca3 --- /dev/null +++ "b/\351\231\210\345\251\267\345\251\267/20230217--http\346\250\241\345\235\227.md" @@ -0,0 +1,32 @@ +# http模块 +是Node.js 官方提供的、用来创建 web 服务器的模块。 + +1.http协议:为请求响应式的协议 +2.http协议常见的方法有:get,post,put,delete,options +3.http的默认端口号是80 +4.websocket简称ws是即时通信协议,其实也是基于http协议 +5.默认的http是不安全的,是明文的 +6.后面新增加的http是安全的,还加密的 + + +## 创建最基本的 web 服务器 + +1.导入 http 模块 +2.创建 web 服务器实例 +3.为服务器实例绑定 request 事件,监听客户端的请求 +4.启动服务器 + +```js +const http=require('http'); + +const server=http.createServer(); + +server.on('request',function (req,res) { + console.log('Someone visit our web server'); +}) + +server.listen(8080,function(){ + console.log('server running at http://127.0.0.1:8080') +}) +``` + -- Gitee