From bdf5c4d4ccb7f6b512c2bd740ddc7b97649d16bb Mon Sep 17 00:00:00 2001 From: 666 <66> Date: Mon, 4 Mar 2024 17:28:43 +0800 Subject: [PATCH 1/5] bj --- "\347\274\252\346\242\223\344\272\250/test.js" | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 "\347\274\252\346\242\223\344\272\250/test.js" diff --git "a/\347\274\252\346\242\223\344\272\250/test.js" "b/\347\274\252\346\242\223\344\272\250/test.js" new file mode 100644 index 0000000..3fc7b07 --- /dev/null +++ "b/\347\274\252\346\242\223\344\272\250/test.js" @@ -0,0 +1,13 @@ +import fs from 'fs'; + +let arr = fs.readdirSync('./'); + +console.log(arr); + +function listArr(filePath) { + // 读取所有的内容 + + // 将所有内容插入到结果数组 + + // 遍历所有内容,检查是否有目录,如果有,则继续调用listArr函数,将函数执行的结果合并到结果数组中 +} \ No newline at end of file -- Gitee From 6c824ccf8e71fd20a3b2c1290d542f3b0adf22b3 Mon Sep 17 00:00:00 2001 From: xxx Date: Mon, 4 Mar 2024 23:42:40 +0800 Subject: [PATCH 2/5] bj --- .../20240304-fs.md" | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) rename "\347\274\252\346\242\223\344\272\250/test.js" => "\347\274\252\346\242\223\344\272\250/20240304-fs.md" (32%) diff --git "a/\347\274\252\346\242\223\344\272\250/test.js" "b/\347\274\252\346\242\223\344\272\250/20240304-fs.md" similarity index 32% rename from "\347\274\252\346\242\223\344\272\250/test.js" rename to "\347\274\252\346\242\223\344\272\250/20240304-fs.md" index 3fc7b07..75e04b3 100644 --- "a/\347\274\252\346\242\223\344\272\250/test.js" +++ "b/\347\274\252\346\242\223\344\272\250/20240304-fs.md" @@ -1,13 +1,34 @@ -import fs from 'fs'; -let arr = fs.readdirSync('./'); +### 我还在补前天的知识 -console.log(arr); +```js +import fs from 'fs'; function listArr(filePath) { // 读取所有的内容 - + let res = [] + fs.readdir('./',(err,data)=>{ + data.forEach(item=>{ + console.log(item); + fs.stat(item,(f,t)=>{ + let isDir = t.isDirectory() + if(isDir){ + fs.readdir(item,(x,y)=>{ + if(x){ + console.log(x); + }else{ + console.log(y); + } + }) + } + }) + }) + }) + // 将所有内容插入到结果数组 // 遍历所有内容,检查是否有目录,如果有,则继续调用listArr函数,将函数执行的结果合并到结果数组中 -} \ No newline at end of file +} + +listArr() +``` \ No newline at end of file -- Gitee From 352cc0bdb44572cc45fb8f04fe0df15b273d47f2 Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 7 Mar 2024 11:35:12 +0800 Subject: [PATCH 3/5] bj --- ...07\344\273\266\346\250\241\345\235\227.md" | 0 .../20240304-fs.md" | 36 +++++++----------- .../20240305-http\346\250\241\345\235\227.md" | 37 +++++++++++++++++++ 3 files changed, 50 insertions(+), 23 deletions(-) rename "\347\274\252\346\242\223\344\272\250/20240303-js\346\226\207\344\273\266\346\250\241\345\235\227.md" => "\347\274\252\346\242\223\344\272\250/20240303-fs\346\226\207\344\273\266\346\250\241\345\235\227.md" (100%) create mode 100644 "\347\274\252\346\242\223\344\272\250/20240305-http\346\250\241\345\235\227.md" diff --git "a/\347\274\252\346\242\223\344\272\250/20240303-js\346\226\207\344\273\266\346\250\241\345\235\227.md" "b/\347\274\252\346\242\223\344\272\250/20240303-fs\346\226\207\344\273\266\346\250\241\345\235\227.md" similarity index 100% rename from "\347\274\252\346\242\223\344\272\250/20240303-js\346\226\207\344\273\266\346\250\241\345\235\227.md" rename to "\347\274\252\346\242\223\344\272\250/20240303-fs\346\226\207\344\273\266\346\250\241\345\235\227.md" diff --git "a/\347\274\252\346\242\223\344\272\250/20240304-fs.md" "b/\347\274\252\346\242\223\344\272\250/20240304-fs.md" index 75e04b3..6b9f2cb 100644 --- "a/\347\274\252\346\242\223\344\272\250/20240304-fs.md" +++ "b/\347\274\252\346\242\223\344\272\250/20240304-fs.md" @@ -2,33 +2,23 @@ ### 我还在补前天的知识 ```js -import fs from 'fs'; +import fs, { readdirSync, statSync } from 'fs'; function listArr(filePath) { // 读取所有的内容 let res = [] - fs.readdir('./',(err,data)=>{ - data.forEach(item=>{ - console.log(item); - fs.stat(item,(f,t)=>{ - let isDir = t.isDirectory() - if(isDir){ - fs.readdir(item,(x,y)=>{ - if(x){ - console.log(x); - }else{ - console.log(y); - } - }) - } - }) - }) - }) - - // 将所有内容插入到结果数组 - + // 将所有内容插入到结果数组 + let patharr = readdirSync(filePath); // 遍历所有内容,检查是否有目录,如果有,则继续调用listArr函数,将函数执行的结果合并到结果数组中 + patharr.forEach(item=>{ + const path = filePath + item; + const filestat = fs.statSync(path); + if(filestat.isDirectory()){ + res.push(...listArr(path+'/')); + } + res.push(item); + }) + return res; } - -listArr() +console.log(listArr('./')); ``` \ No newline at end of file diff --git "a/\347\274\252\346\242\223\344\272\250/20240305-http\346\250\241\345\235\227.md" "b/\347\274\252\346\242\223\344\272\250/20240305-http\346\250\241\345\235\227.md" new file mode 100644 index 0000000..c9684ff --- /dev/null +++ "b/\347\274\252\346\242\223\344\272\250/20240305-http\346\250\241\345\235\227.md" @@ -0,0 +1,37 @@ +### nodejs项目保存重启 +- win+R打开powershell +- start-process powershell -verb runas +- set-executionpolicy remotesigned + +### 练习 + +```js +import happy from "http"; +import fs from "fs" + +let app = happy.createServer(function (req, res) { + res.setHeader('content-type', 'text/html;charset=utf-8') + const url = req.url; + console.log(url); + if (url) { + fs.readFile(`./${url}.html`, (err, data) => { + if (err) { + fs.readFile('./default.html', (err, data) => { + if (err) { + res.end("404"); + return; + } + res.end(data) + }) + return; + } + res.end(data) + }) + } +}) + + +app.listen(3000) + +console.log('已经成功打开 http://127.0.0.1:3000/index'); +``` \ No newline at end of file -- Gitee From aa163e7ead8afc72da6725b7c3b3c184641fb130 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 8 Mar 2024 10:36:01 +0800 Subject: [PATCH 4/5] bj --- ...34\344\270\232\350\256\262\350\257\204.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "\347\274\252\346\242\223\344\272\250/20240307-\344\275\234\344\270\232\350\256\262\350\257\204.md" diff --git "a/\347\274\252\346\242\223\344\272\250/20240307-\344\275\234\344\270\232\350\256\262\350\257\204.md" "b/\347\274\252\346\242\223\344\272\250/20240307-\344\275\234\344\270\232\350\256\262\350\257\204.md" new file mode 100644 index 0000000..231e3aa --- /dev/null +++ "b/\347\274\252\346\242\223\344\272\250/20240307-\344\275\234\344\270\232\350\256\262\350\257\204.md" @@ -0,0 +1,68 @@ +## 解题思路 +- 1.通过request.url拿到路径 + +- 2.拿到路径双后尝试和index.html拼接,尝试去读取这个路径的文件 + +- 方法1:判断拼接后的路径是否存在,如果存在,则读取文件内容返回给请求端 + +- 方法2:直接去读取,如果报错,而且报路径找不到的错误,我们就知道这个文件不存在,则做没有文件的处理 + +### 需求:根据地址栏不同的路径,读取对应下的index.html文件,通过响应返回给请求端,如果没有找到文件,则显示“很遗憾” + +```js +'use strict'; + +import fs from 'fs'; + +import http from 'http'; + +let tips='很遗憾'; + +let app=http.createServer(function(request,response) +{ + let url=request.url; + // console.log(url); + + // url有两种情况: + // ① 它直接就是根目录,其值为"/",根据拼接要求后面跟index.html,故拼接后的文件路径类似于://index.html + // ② 它从根目录开始,接一个指定路径这个时候其值类似于"/abc",拼接后的文件路径类似:"/abc/index.html" + + + let filePath='.'+url+'/'+'index.html'; + // console.log(filePath); + + response.setHeader('content-type','text/html;charset=utf-8'); + + // 异步方法 + fs.readFile(filePath,function(err,data) + { + if(err) + { + response.end(tips); + } + else + { + response.end(data); + } + }); + + + // // 同步方法 + // try + // { + // let content=fs.readFileSync(filePath,'utf-8'); + // response.end(content); + // } + // catch(error) + // { + // console.log(error); + // response.end(tips); + // } +}); + +let port=3000; + +app.listen(port); + +console.log(`http服务器运行于如下地址:http://localhost:${port}`); +``` \ No newline at end of file -- Gitee From 7f8587892e1596eb2db6f4f3b9bab447a21546ad Mon Sep 17 00:00:00 2001 From: xxx Date: Sun, 10 Mar 2024 12:58:28 +0800 Subject: [PATCH 5/5] bj --- ...62\351\242\230\346\200\235\350\267\257.md" | 69 +++++++++++++++++++ .../20240308-koa\344\273\213\347\273\215.md" | 65 +++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 "\347\274\252\346\242\223\344\272\250/20240307-http\346\250\241\345\235\227\350\256\262\351\242\230\346\200\235\350\267\257.md" create mode 100644 "\347\274\252\346\242\223\344\272\250/20240308-koa\344\273\213\347\273\215.md" diff --git "a/\347\274\252\346\242\223\344\272\250/20240307-http\346\250\241\345\235\227\350\256\262\351\242\230\346\200\235\350\267\257.md" "b/\347\274\252\346\242\223\344\272\250/20240307-http\346\250\241\345\235\227\350\256\262\351\242\230\346\200\235\350\267\257.md" new file mode 100644 index 0000000..6048955 --- /dev/null +++ "b/\347\274\252\346\242\223\344\272\250/20240307-http\346\250\241\345\235\227\350\256\262\351\242\230\346\200\235\350\267\257.md" @@ -0,0 +1,69 @@ +## http模块 + +### 需求:根据地址栏不同的路径,读取对应下的index.html文件,通过响应返回给请求端,如果没有找到文件,则显示“很遗憾” + +```js +'use strict'; + +import fs from 'fs'; + +import http from 'http'; + +// 1.通过request.url拿到路径 + +// 2.拿到路径双后尝试和index.html拼接,尝试去读取这个路径的文件 + +// 方法1:判断拼接后的路径是否存在,如果存在,则读取文件内容返回给请求端 + +// 方法2:直接去读取,如果报错,而且报路径找不到的错误,我们就知道这个文件不存在,则做没有文件的处理 + +let tips='很遗憾'; + +let app=http.createServer(function(request,response) +{ + let url=request.url; + // console.log(url); + + // url有两种情况: + // ① 它直接就是根目录,其值为"/",根据拼接要求后面跟index.html,故拼接后的文件路径类似于://index.html + // ② 它从根目录开始,接一个指定路径这个时候其值类似于"/abc",拼接后的文件路径类似:"/abc/index.html" + + + let filePath='.'+url+'/'+'index.html'; + // console.log(filePath); + + response.setHeader('content-type','text/html;charset=utf-8'); + + // 异步方法 + fs.readFile(filePath,function(err,data) + { + if(err) + { + response.end(tips); + } + else + { + response.end(data); + } + }); + + + // // 同步方法 + // try + // { + // let content=fs.readFileSync(filePath,'utf-8'); + // response.end(content); + // } + // catch(error) + // { + // console.log(error); + // response.end(tips); + // } +}); + +let port=3000; + +app.listen(port); + +console.log(`http服务器运行于如下地址:http://localhost:${port}`); +``` \ No newline at end of file diff --git "a/\347\274\252\346\242\223\344\272\250/20240308-koa\344\273\213\347\273\215.md" "b/\347\274\252\346\242\223\344\272\250/20240308-koa\344\273\213\347\273\215.md" new file mode 100644 index 0000000..07f0b26 --- /dev/null +++ "b/\347\274\252\346\242\223\344\272\250/20240308-koa\344\273\213\347\273\215.md" @@ -0,0 +1,65 @@ +## koa +- koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 + +## 历史 +Express +Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: +```js +var express = require('express'); +var app = express(); + +app.get('/', function (req, res) { + res.send('Hello World!'); +}); + +app.listen(3000, function () { + console.log('Example app listening on port 3000!'); +}); + + +``` + +## promise的构造函数 + +- Promise 构造函数是 JavaScript 中用于创建 Promise 对象的内置构造函数。 + +- Promise 构造函数接受一个函数作为参数,该函数是同步的并且会被立即执行,所以我们称之为起始函数。起始函数包含两个参数 resolve 和 reject,分别表示 Promise 成功和失败的状态。 + +- 起始函数执行成功时,它应该调用 resolve 函数并传递成功的结果。当起始函数执行失败时,它应该调用 reject 函数并传递失败的原因。 + +- Promise 构造函数返回一个 Promise 对象,该对象具有以下几个方法: + +- then:用于处理 Promise 成功状态的回调函数。 +- catch:用于处理 Promise 失败状态的回调函数。 +- finally:无论 Promise 是成功还是失败,都会执行的回调函数。 +下面是一个使用 Promise 构造函数创建 Promise 对象的例子: + +当 Promise 被构造时,起始函数会被同步执行: + +```js +const promise = new Promise((resolve, reject) => { + // 异步操作 + setTimeout(() => { + if (Math.random() < 0.5) { + resolve('success'); + } else { + reject('error'); + } + }, 1000); +}); + +promise.then(result => { + console.log(result); +}).catch(error => { + console.log(error); +}); + +``` + +在上面的例子中,我们使用 Promise 构造函数创建了一个 Promise 对象,并使用 setTimeout 模拟了一个异步操作。如果异步操作成功,则调用 resolve 函数并传递成功的结果;如果异步操作失败,则调用 reject 函数并传递失败的原因。然后我们使用 then 方法处理 Promise 成功状态的回调函数,使用 catch 方法处理 Promise 失败状态的回调函数。 + +这段程序会直接输出 error 或 success。 + +resolve 和 reject 都是函数,其中调用 resolve 代表一切正常,reject 是出现异常时所调用 + +Promise 类有 .then() .catch() 和 .finally() 三个方法,这三个方法的参数都是一个函数,.then() 可以将参数中的函数添加到当前 Promise 的正常执行序列,.catch() 则是设定 Promise 的异常处理序列,.finally() 是在 Promise 执行的最后一定会执行的序列。 \ No newline at end of file -- Gitee