diff --git "a/\346\264\252\351\223\266\350\212\261/20230220-Nodehttp2.md" "b/\346\264\252\351\223\266\350\212\261/20230220-Nodehttp2.md" new file mode 100644 index 0000000000000000000000000000000000000000..7a778de435237cc6b5c11e21955c7e708576401f --- /dev/null +++ "b/\346\264\252\351\223\266\350\212\261/20230220-Nodehttp2.md" @@ -0,0 +1,20 @@ + +``` +let http = require('http'); +let fs = require('fs'); + +let app = http.createServer((req,res) => { + let url = '.' + req.url; //文件名,'.'代表当前目录 + fs.readFile(url,'utf-8',('err,data') => { + if(err){ + res.end('404'); + }else{ + res.end(data); + } + }) +}); + +app.listen(80,() => { + console.log('server running http://127.0.0.1'); //输出出来 +}); +``` \ No newline at end of file diff --git "a/\346\264\252\351\223\266\350\212\261/20230221-\345\270\203\347\275\256\351\235\231\346\200\201\347\275\221\347\253\231.md" "b/\346\264\252\351\223\266\350\212\261/20230221-\345\270\203\347\275\256\351\235\231\346\200\201\347\275\221\347\253\231.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a790b11beb60be50fe8dbb658949384e056812c --- /dev/null +++ "b/\346\264\252\351\223\266\350\212\261/20230221-\345\270\203\347\275\256\351\235\231\346\200\201\347\275\221\347\253\231.md" @@ -0,0 +1,20 @@ +ssh root@ + + 进入服务器后 + 1.进入/var/www/ 目录: cd /var/www/ + 2.在目录中创建于域名相同的文件夹:mkdir t1.tongjunhao.top; + 3.在vscode上传本地文件到刚刚创建的ti.tongjunhao.top文件夹里: scp .\index.html root@tongjunhao.top:/var/www/t1.tongjunhao.top/ + 4.进入/etc/nginx/conf.d/目录:cd /etc/nginx/conf.d/ + 5.配置nginx文件:vim 域名.conf + 6. server{ + listen 80; + server_name t1.tongjunhao.top; + location / { + root /var/www/t1.tongjunhao.top; + index index.html; + } + } + 配置完保存退出 ESC :wq! + 7.测试:nginx -t + 8.重启:nginx -s reload; + 这样就布置好一个静态网页了!!!! \ No newline at end of file diff --git "a/\346\264\252\351\223\266\350\212\261/20230223-http3.md" "b/\346\264\252\351\223\266\350\212\261/20230223-http3.md" new file mode 100644 index 0000000000000000000000000000000000000000..6ab4de15b7aed0459b630c8fd49830198be2e901 --- /dev/null +++ "b/\346\264\252\351\223\266\350\212\261/20230223-http3.md" @@ -0,0 +1,92 @@ +## 讲解 +``` +'use strict' +//1、引入需要的模块 +const aa =require('aa') +const http = require('http'); +const { resolve } = require('path'); + + +//有一个函数或者功能,返回一些数据,这些数据是已经经过判断处理的 +function readFile(url) { + //解决异步处理文件,返回内容总是还未处理的结果的问题 + + //方法1 + //同步读取文件,解决上述问题 + //resultData = fa.readFileSync('./xxx.html','utf-8'(转码)); + + //方法2 + //promise的方法来解决上述问题 + return new Promise((resolve,reject)=>{ + //readFile 从文件中读取数据 + fs.readFile(url,'uft8',(err,data)=>{ + if (err) { + reject(err); + }else{ + console.log(data); + resolve(data); + } + + }) + }); +} + //判断传入的路径是否存在,是则返回判断是否文件夹的true,否则返回false,最终通过promise对象返回 + function isDor(filePath) { + return new Promise((resolve,reject)=>{ + fs.stat(filePath,(err,data)=>{ + if (err) { + reject(err); + }else{ + //isDirectory 在fs方法中运用:如果特定目录描述Directory,则此方法返回true,否则返回false + if (data.isDirectory()) { + resolve(true); + }else{ + resolve(false) + } + } + }) + }) + } +//3、拿到上一步返回的数据,交给http模块创建的web服务展示 +let bb =http.createServer((request,response) => { + //防止中文乱码 + response.writeHead(200,{'Content-Type':'xx/html;charset=utf8'}) + let url ='.'+request.url; + isDor(url).then(res =>{ + console.log(res); + if (res === ture) { + readFile(url + '/xx.html').then(res =>{ + response.end(res); + }).catch(err =>{ + console.log(err); + }); + } + + + + + + + }) +}) +//createServer 方法说明: +//该函数用来创建一个HTTP服务器,并将 onRequest 作为 request 事件的监听函数。 +//也就是说,当用户请求http服务器时,用户会像服务器发送一个 onRequest函数。并且函数会传递两个参数 +//接收参数: +// request 请求对象,想知道req有哪些属性,可以查看 “http.request 属性整合”。 +// response 响应对象 ,收到请求后要做出的响应。想知道response有哪些属性,可以查看 “http.response属性整合” + +//writeHead 响应头 +///向请求的客户端发送响应头。 +//该函数在一个请求内最多只能调用一次, +//如果不调用,则会自动生成一个响应头。 + +let port = 8088; +bb.listen(port); +console.log(`服务器运行如下地址: http:/127.0.0.1:${port}`); + +// 127.0.0.1代表本级地址 +//localhost 被所有的机器认可,绑定为127.0.0.1 + + +``` \ No newline at end of file diff --git "a/\346\264\252\351\223\266\350\212\261/20230224-Nodehttp+koa.md" "b/\346\264\252\351\223\266\350\212\261/20230224-Nodehttp+koa.md" new file mode 100644 index 0000000000000000000000000000000000000000..4200ec04be1062f1b7ae2b3a46f783880890345e --- /dev/null +++ "b/\346\264\252\351\223\266\350\212\261/20230224-Nodehttp+koa.md" @@ -0,0 +1,30 @@ +## async await 讲解 +### 转换为promise对象 +![图裂了](./img/1.JPG) +![图裂了](./img/2.JPG) + +## koa +``` +//导入koa模块 +const Koa =require('koa'); + +//创建一个koa对象 +let aa=new Koa(); + +//调用 +//ctx表示上下文 +aa.use(async (ctx, next) =>{ + await next(); + ctx.response.type = 'text/html'; + ctx.response.body='hi'; +}); + +//端口监听 +aa.listen(3000); +console.log(`服务器运行如下地址: http://127.0.0.1:3000`); +``` +### 下载koa +yarn add koa + + +npm install koa \ No newline at end of file diff --git "a/\346\264\252\351\223\266\350\212\261/img/1.JPG" "b/\346\264\252\351\223\266\350\212\261/img/1.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..1bc2d1d0efb06be78d078ef18a2a54c98e8d968f Binary files /dev/null and "b/\346\264\252\351\223\266\350\212\261/img/1.JPG" differ diff --git "a/\346\264\252\351\223\266\350\212\261/img/2.JPG" "b/\346\264\252\351\223\266\350\212\261/img/2.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..9b096174ee2fef3a119c4b708559d5ba37341d11 Binary files /dev/null and "b/\346\264\252\351\223\266\350\212\261/img/2.JPG" differ