2023.4.4的課程講義
url怎么用
url是什么
url.parse----解析url
var url=require('url')
url.parse("網(wǎng)址")
網(wǎng)址:http://www.baidu.com
? ?var url=require('url')
? ?url.parse("http://www.baidu.com?name=good")
? ?url.parse("http://www.baidu.com?name=good",true)
--------------------------------------------
const url=require('url');
var api="http://www.baidu.com?name=nice&age=18";
console.log(url.parse(api));
// ?query: 'name=nice&age=18'
--------------------------------------------
//
const url=require('url');
var api="http://www.baidu.com?name=nice&age=18";
console.log(url.parse(api,true));
// query: [Object: null prototype] { name: 'nice', age: '18' },
//轉(zhuǎn)換成了對象!
-------------------------------------------
? ?//
學(xué)習(xí)這個:!
var getValue=url.parse(api,true).query;
console.log(getValue);
怎么寫:
const url=require('url');
var api="http://www.baidu.com?name=nice&age=18";
var getValue=url.parse(api,true).query;
console.log(getValue);
-------------------------------------------
//模板字符串
const url=require('url');
var api="http://www.baidu.com?name=nice&age=18";
var getValue=url.parse(api,true).query;
console.log(getValue);
console.log(`姓名:${getValue.name}---年齡:${getValue.age}`);
------------------------------------------
? ?作者:燒串者 https://www.bilibili.com/read/cv22828244?spm_id_from=333.999.0.0 出處:bilibili
const http=require('http');
http.createServer((req,res)=>{
? ? ? ?console.log(req.url);
? ? ? ?res.writeHead(200,{"Content-type":"text/html;charset='utf-8'"}); //解決亂碼
? ? ? ?res.write("<head> <meta charset='UTF-8'></head>"); ?//解決亂碼
? ? ? ?(?)
? ? ? ?res.end("中華民族偉大復(fù)興");
? ?}
).listen(3001);
console.log("server running at localhost:3001");
[-----------------------
? ?http://www.baidu.com?name=nice&age=18
? -------> 127.0.0.1?name=nice&age=18
? ?------------------------]
console.log(req.url);---->127.0.0.1?name=nice&age=18
----------------------------------------
const url=require('url');
if(req.url=='/favicon.ico'){
? ?var userinfo=url.parse(req.url,true).query;
? ?console.log(`姓名:${getValue.name}---年齡:${getValue.age}`);
}
------------------------------------------------------
const http=require('http');
const url=require('url');
http.createServer((req,res)=>{
? ? ? ?console.log(req.url);
? ? ? ?res.writeHead(200,{"Content-type":"text/html;charset='utf-8'"}); //解決亂碼
? ? ? ?res.write("<head> <meta charset='UTF-8'></head>"); ?//解決亂碼
? ? ? ?if(req.url!=='/favicon.ico'){
? ? ? ? ? ?var userinfo=url.parse(req.url,true).query;
? ? ? ? ? ?console.log(`姓名:${userinfo.name}---年齡:${userinfo.age}`);
? ? ? ?}
? ? ? ?res.end("中華民族偉大復(fù)興");
? ?}
).listen(3001);
console.log("server running at localhost:3001");