//解析url
let url=require("url"); let strUrl="https://ss1.baidu.com:443/6ONXsjip0QIZ8tyhnq/it/index.html?fm=58&s=1AAA7A23C4A4D9031E5D94C60000C0B1&bpow=121&bpoh=75#dd"; let obj=url.parse(strUrl); console.log(obj); /* Url { protocol: 'https:', 传输协议 slashes: true, 是否有斜杠 auth: null, 作者 host: 'ss1.baidu.com:443', 域名+端口号 port: '443', 端口号 hostname: 'ss1.baidu.com', 域名 hash: '#dd', 哈希值 search: '?fm=58&s=1AAA7A23C4A4D9031E5D94C60000C0B1&bpow=121&bpoh=75', 查询字符串 ?+参数 query: 'fm=58&s=1AAA7A23C4A4D9031E5D94C60000C0B1&bpow=121&bpoh=75', 参数 pathname: '/6ONXsjip0QIZ8tyhnq/it/index.html', 文件路径名 path: '/6ONXsjip0QIZ8tyhnq/it/index.html?fm=58&s=1AAA7A23C4A4D9031E5D94C60000C0B1&bpow=121&bpoh=75', href: 'https://ss1.baidu.com:443/6ONXsjip0QIZ8tyhnq/it/index.html?fm=58&s=1AAA7A23C4A4D9031E5D94C60000C0B1&bpow=121&bpoh=75' } */ let obj1=url.parse(strUrl,true);//将query解析成成对象的形式 console.log(obj1); /* query:{fm: '58', s: '1AAA7A23C4A4D9031E5D94C60000C0B1', bpow: '121', bpoh: '75' }, */ let {query,pathname}=obj1;
-----------------------------------------------
http.createServer((req,res)=>{ //req.url 客户端请求的地址 //根据地址获取请求的文件路径 let {pathname}=url.parse(req.url); //根据当前pathname去读取出文件 //"/index.html" "/list.html" let result=fs.readFileSync("."+pathname,"utf-8"); //将读取的内容返回给客户端 //res.end(返回的内容必须是字符串) res.end(result); }).listen(4567,()=>{ console.log("OK"); });
-----------------------------------------------------------req.on("data",()=>{}//数据在传递过程中触发的函数 req.on("end",()=>{}//数据传完了触发的函数
* 页面请求有两种方式 * 1.真实文件路径 请求资源文件,html,.css,.js,img,..... * 2.伪地址 请求数据 基本上是ajax请求 * /aa