日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Node.js基礎(chǔ)學(xué)習(xí)(1)

 路人甲Java 2021-09-08
 1 //用于創(chuàng)建網(wǎng)站服務(wù)器的模塊
 2 const http = require('http');
 3 //app對象就是網(wǎng)站服務(wù)器對象
 4 const app = http.createServer();
 5 // node內(nèi)置對象 用于處理URL地址
 6 const url = require('url');
 7 
 8 //事件(請求)處理函數(shù),當(dāng)客戶端有請求的時(shí)候
 9 //request請求,response響應(yīng)
10 app.on('request',(req,res)=>{
11     //獲取請求的方式
12     //req.method
13     // console.log(req.method);    
14 
15     //獲取請求的地址
16     //req.url
17     console.log(req.url);
18     //第一個(gè)參數(shù)要解析的url地址,第二個(gè)參數(shù)為true時(shí) 解析當(dāng)前請求地址已對象的形式返回 
19     // console.log(url.parse(req.url,true).query);
20 
21     // req.headers
22     // console.log(req.headers['accept']); 
23 
24     // 用解構(gòu)賦值的形式拿到當(dāng)前url里面的query,pathname
25     let {query,pathname}=url.parse(req.url,true);
26     console.log(query.name);
27     console.log(query.age);
28 
29 
30     res.writeHead(200, {
31         'content-type': 'text/html;charset=utf8'
32     });
33     
34    
35     if(pathname=='/index' || pathname=='/' ){
36         res.end('<h2>welcome to homepage</h2>');
37     }else if(pathname=='/list'){
38         res.end('welcome to listpage');
39     }else{
40         res.end('404');
41     }
42     
43 
44     // if(req.method== 'POST'){
45     //     res.end('post');
46     // }else if (req.method== 'GET'){
47     //     res.end('get');
48     // }
49     //響應(yīng)內(nèi)容
50     // res.end('<h2>hello user</h2>')
51 
52 });
53 
54 //監(jiān)聽端口
55 app.listen(3000);
56 console.log('網(wǎng)站服務(wù)啟動(dòng)成功!')
57 
58 // 訪問本地 localhost:3000

 

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多