关于IO理解
IO:数据的传输
input 输入
output 输出
关于进程与线程,队列,线程池理解,参考我的另一篇博客:https://blog.csdn.net/qq_28835447/article/details/94121838
异步的io操作和异步的非io操作理解:只要有数据的传输都是IO操作
异步的IO操作:
<link rel="stylesheet" type="text/css" href="css/index.css"/>
<img src= "./../img/img" />
异步script 加载
异步的非IO操作:
settimeout setIneterval
同步异步的区别?
同步会阻塞代码,异步不会阻塞代码
一条线程先执行同步的代码,主线程会调度线程池中的子线程去执行异步的代码,异步处理的结果什么时候完成什么时候再回到主线程中
什么是并发
一段时间内有多个程序在运行与运行完毕之间
path模块
const http = require('path')
path.basename(path)//文件名加后缀
path.basename(path,'.后缀') //第二个参数指定去除的后缀名
path.dirname(path)//仅仅只返回绝对路径中的文件路径部分,不包含文件
path.extname(path)//返回文件后缀\a\b\c
path.join('/a', "/b", "/c") //将给定的path片段拼接起来 \a\b\c
path.join('/a', '/b', '///c', '..') // \a\b 在最后加.. 则会返回上一级
path.parse(__filename) //将路径解析为对象
{
root: 'C:\\',
dir: 'C:\\Users\\Administrator\\Desktop\\node+MongoDB\\node\\02',
base: '05.js',
ext: '.js',
name: '05'
}
url模块
let str = 'https://baidu.com:8080/p/a/t/h?id=1&name=美女'; let url = require('url'); console.log(url.parse(str, true)); Url { protocol: 'https:', slashes: true, auth: null, host: 'baidu.com:8080', port: '8080', hostname: 'baidu.com', hash: null, search: '?id=1&name=美女', query: { id: '1', name: '美女' }, pathname: '/p/a/t/h', path: '/p/a/t/h?id=1&name=美女', href: 'https://baidu.com:8080/p/a/t/h?id=1&name=美女'
}
http模块
引入http模块搭建服务器
const http = require('http') //引入模块,用来提供服务
const server = http.createServer(); //创建服务,用一个参数接受
server.on('request',function(request,response){ //监听请求对应的请求和响应,监听request事件,当有请求发送的时候,都会触发request事件,触发完成就会去执行一个callback
response.writeHead(200,{'content-type':"text/html;charset=utf8"}); //响应头,
response.write('<h1>你好</h1>'); //必须现有writeHead 在用write
response.end('world') //结束,结束必须有,表示响应和请求已经结束
console.log(request.httpVersion);//描述HTTP协议版本,通常是1.0或者1.1
console.log(request.method) //描述HTTP请求方法,比如GET,POST,PUT,DELETE等
console.log(request.url) //描述原始的请求路径
})
server.listen(3000) //监听端口
request:请求对象形参
response:响应对象形参
response.writeHead(200,{'content-type':"text/html;charset=utf8"}); 请求头,对应的文本,图片,JavaScript,css等都有与之对应
例子:
//引入模块 const fs = require('fs') const http = require('http'); const path = require('path'); const querystring = require('querystring'); const url = require('url'); const server = http.createServer();//创建服务 server.on('request',function(req,res){ //绑定请求事件 let urlObj = url.parse(req.url,true); //将请求地址转为对象 let query = urlObj.query; //请求参数 let pathname = urlObj.pathname //请求地址
//接口文档的编写
if (pathname == '/getData' && req.method == 'GET') { //当为GET请求时 res.end('0') } else if(pathname = '/index' && req.method == 'POST'){ //当为POST请求时 var data = ""; //作为容器 ,存储请求参数 req.on('data',function(chunk){ //绑定监听请求参数 console.log(chunk) //参数以buffer对象 data +=chunk }) req.on('end',function(){ //绑定请求结束时 let obj = querystring.parse(data); //将buffer对象打包为对象转为对象字符串形式 console.log(obj) res.end("1") }) } }) server.listen(3000);
node简单搭建静态伺服
//引入模块
const http = require("http"); const fs = require("fs"); const url = require("url"); const path = require("path"); const querystring = require("querystring"); const server = http.createServer();
//创建服务 server.on('request',(req,res) =>{ let urlObj = url.parse(req.url,true) let query = urlObj.query; let pathname = urlObj.pathname; let query_uname = query.uname; //静态伺服部分 if(pathname == "/register.html" && req.method == "GET"){ fs.readFile("./src/register.html","utf8",(err,data) => { if(err){ res.end("404"); return; } res.writeHead(200,{'content-type':"text/html;charset=utf8"}); res.end(data); }) } else if(pathname == "/css/register.css" && req.method == "GET"){ fs.readFile("./src/css/register.css","utf8",(err,data) => { if(err){ res.end("404"); return; } res.writeHead(200,{'content-type':"text/css;charset=utf8"}); res.end(data); }) } else if(pathname == "/js/register.js" && req.method == "GET"){ fs.readFile("./src/js/register.js","utf8",(err,data) => { if(err){ res.end("404"); return; } res.writeHead(200,{'content-type':"text/javascript;charset=utf8"}); res.end(data); }) }
})
//监听端口
server.listen(3000)
封装写法
const http = require("http"); const fs = require("fs"); const url = require("url"); const path = require("path"); const querystring = require("querystring"); const server = http.createServer(); server.on('request',(req,res) =>{ let urlObj = url.parse(req.url,true) let query = urlObj.query; let pathname = urlObj.pathname; let query_uname = query.uname; //静态伺服封装 function jintai(pathname,method){ let getext = path.extname(pathname); //文件后缀名 if (method == 'GET') { if (getext == ".html") { getext = ""; } if (getext == 'css' ||'js'||"jpg" ||"png" || "gif" ||"html") { fs.readFile("./src"+pathname,"utf8",(err,data) => { if(err){ res.end("404"); return; } res.writeHead(200,{'content-type':getExt(pathname)}); res.end(data); }) } } } jintai(pathname,req.method) //兼容文件格式 function getExt(pathname){ var getext = path.extname(pathname);//文件的后缀名 var ext = '' if(getext == "html"){ ext = "text/html;charset=utf8"; } if(getext == "css"){ ext = "text/css;charset=utf8"; } if(getext == "jpg"){ ext = "text/jpg;charset=utf8"; } if(getext == "png"){ ext = "text/png;charset=utf8"; } if(getext == "js"){ ext = "text/javascript;charset=utf8"; } if(getext == "gif"){ ext = "text/gif;charset=utf8"; } if(getext == "xml"){ ext = "text/xml;charset=utf8"; } return ext; } }) server.listen(3000)