node.js學習進程2(url)

來源:互聯網
上載者:User

標籤:parse   null   識別   host   course   格式   瀏覽器   form   url   

url三個方法。url.parse(),解析url地址;url.formate(),將一個url對象格式化為url字串;url.resolve(),接受兩個參數,將兩個參數拼接為瀏覽器可以識別的格式。

1、node環境中輸入url,輸出url的上述三個方法。

2、url.parse(‘http://imooc.com/course/list‘)

解析結果:

{

protocol:‘http:’,/*底層協議*/

slashes:true,/*是否有協議的雙斜線*/

auth:null,

host:‘imooc.com‘,/*網域名稱或ip地址*/

port:null,/*連接埠號碼*/

hostname:‘imooc.com‘,

hash:null,

search:null;

query:null,

pathname:‘/course/list‘,

path:‘/course/list‘,

href:‘http://imooc.com/course/list‘

};

url.parse(‘http://imooc.com:8080/course/list?from=scott&course=node#floor1‘)

格式化結果:

{

protocol:‘http:’,/*底層協議*/

slashes:true,/*是否有協議的雙斜線*/

auth:null,

host:‘imooc.com:8080‘,/*網域名稱或ip地址*/

port:8080,/*連接埠號碼*/

hostname:‘imooc.com‘,

hash:floor1,

search:‘?from=scott&course=node’;

query:‘from=scott&course=node’,

pathname:‘/course/list‘,

path:‘/course/list‘,

href:‘http://imooc.com:8080/course/list?from=scott&course=node#floor1‘

}

3、url.format(

{

protocol:‘http:’,/*底層協議*/

slashes:true,/*是否有協議的雙斜線*/

auth:null,

host:‘imooc.com:8080‘,/*網域名稱或ip地址*/

port:8080,/*連接埠號碼*/

hostname:‘imooc.com‘,

hash:floor1,

search:‘?from=scott&course=node’;

query:‘from=scott&course=node’,

pathname:‘/course/list‘,

path:‘/course/list‘,

href:‘http://imooc.com:8080/course/list?from=scott&course=node#floor1‘

}

)

格式化結果:

‘http://imooc.com:8080/course/list?from=scott&course=node#floor1‘

4、url.resolve(‘http://imooc.com‘,‘/course/list‘);

拼接結果:

‘http://imooc.com:8080/course/list‘;

5、url.parse()方法還可以傳入兩個參數,預設情況下都為false。第二個參數設為true時,query解析為對象而不再是字串;第三個參數在不知道底層協議時可以傳入來解析地址。

node.js學習進程2(url)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.