国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Nodejs基礎6之HTTP模塊的獲取請求行和請求頭、獲取請求體、獲取請求路徑和查詢字符串、http請求練習、設置HTTP響應報文、http響應練習

這篇具有很好參考價值的文章主要介紹了Nodejs基礎6之HTTP模塊的獲取請求行和請求頭、獲取請求體、獲取請求路徑和查詢字符串、http請求練習、設置HTTP響應報文、http響應練習。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

HTTP模塊

含義 語法 重點掌握
請求方法 request.method *
請求版本 request.httpVersion
請求路徑 request.url *
URL 路徑 require(‘url’).parse(request.url).pathname *
URL 查詢字符串 require(‘url’).parse(request.url, true).query *
請求頭 request.headers *
請求體 request.on(‘data’, function(chunk){}),request.on(‘end’, function(){});

獲取請求行和請求頭

// 1、導入http模塊
const http=require('http')

// 2、創(chuàng)建服務對象
const server=http.createServer((request,response)=>{
    response.end('hello http')  //設置響應體
    // 獲取請求的方法
    console.log("request.method:",request.method)
    // 獲取請求的url
    console.log("request.url:",request.url)  //只包含url中的路徑與查詢字符串
    // 獲取http協(xié)議的版本號
    console.log("request.httpVersion:",request.httpVersion)
    // 獲取http的請求頭
    console.log("request.headers:",request.headers);
    console.log("request.headers.host",request.headers.host)  //獲取單個的請求頭
})

// 3、監(jiān)聽端口,啟動服務
server.listen(9000,()=>{
    console.log('服務已經(jīng)啟動...')
})

瀏覽器發(fā)送請求:
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
終端輸出內(nèi)容:
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

獲取請求體

//1、導入http模塊
const http=require('http')

//2、創(chuàng)建服務對象
const server=http.createServer((request,response)=>{
    //1.聲明一個變量
    let body=''
    //2.綁定data事件
    request.on('data',chunk => {
        body+=chunk  //進行加法運算時候,會將字節(jié)自動轉(zhuǎn)換為字符串進行加法運算
    })
    //3.綁定end事件
    request.on('end',()=>{
        console.log(body)
        //響應
        response.end('httpServer')
    })
})

server.listen(9000,()=>{
    console.log("服務器已經(jīng)啟動了...")
})

form表單

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:9000" method="post">
    <input type="text" name="username"> <br>
    <input type="password" name="password"> <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

  1. 啟動服務
    nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

  2. 瀏覽器打開form表單,輸入賬戶密碼,點擊提交
    nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

  3. 提交之后頁面跳轉(zhuǎn)鏈接

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

  1. 終端輸出url拼接字符串
    nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

獲取請求路徑和查詢字符串

方式一

const http=require('http')
//1、引入url模塊
const url=require('url')

const server = http.createServer((request,response)=>{
    //2、解析request.url
    const res=url.parse(request.url)
    console.log("res",res)
    // 路徑
    console.log("res.pathname",res.pathname);
    // 解析request.url 第二個參數(shù)設置為true
    const resObj=url.parse(request.url,true)
    console.log("resObj",resObj)
    // 查詢字符串
    console.log("resObj.query.keyword",resObj.query.keyword)
    response.end('url')
});

server.listen(9000,()=>{
    console.log("服務器已經(jīng)啟動......")
})

發(fā)送了兩個請求
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
輸出的也是兩個請求的內(nèi)容:
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

方式二

const http=require('http')

const server=http.createServer((request,response)=>{
    // 實例化url對象
    // let url=new URL('/search?a=100&b=200','http://127.0.0.1:9000')
    let url=new URL(request.url,'http://127.0.0.1')
    console.log("url",url)
    //輸出路徑
    console.log("url.pathname",url.pathname)
    //輸出 keyword 查詢字符串
    console.log("url.searchParams.get('keyword')",url.searchParams.get('keyword'))
    response.end('url new')
})

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動...")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

http請求練習

按照以下要求搭建 HTTP 服務

請求類型(方法) 請求地址 響應體結(jié)果
get /login 登錄頁面
get /reg 注冊頁面
const http=require('http')

const server = http.createServer((request,response)=>{
    //獲取請求的方法
    let {method} = request
    //獲取請求的url路徑
    let {pathname}=new URL(request.url,'http://127.0.0.1')
    response.setHeader('content-type','text/html;charset=utf-8')
    //判斷
    if(method==='GET'&&pathname==='/login'){
        //登錄的情形
        response.end('登錄頁面')
    }else if(method==="GET"&&pathname==='/reg'){  //register 注冊
        response.end('注冊頁面')
    }else{
        response.end('Not Found')
    }
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動...")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

設置HTTP響應報文

作用 語法
設置響應狀態(tài)碼 response.statusCode
設置響應狀態(tài)描述 response.statusMessage (用的非常少)
設置響應頭信息 response.setHeader(‘頭名’, ‘頭值’)
設置響應體 response.write(‘xx’)
response.end(‘xxx’)

狀態(tài)碼

const http=require('http')

const server = http.createServer((request,response)=>{
	//1、設置響應狀態(tài)碼
    response.statusCode=205
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js
2.

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    response.statusCode=205
    response.statusCode=404
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

響應狀態(tài)描述

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    response.statusMessage='iloveyou';
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

響應頭

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    response.setHeader('content-type','text/html;charset=utf-8')
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    // response.setHeader('content-type','text/html;charset=utf-8')
    response.setHeader('Server','Node.js')
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    // response.setHeader('content-type','text/html;charset=utf-8')
    // response.setHeader('Server','Node.js')
    response.setHeader('myServer','test test test')
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    // response.setHeader('content-type','text/html;charset=utf-8')
    // response.setHeader('Server','Node.js')
    // response.setHeader('myServer','test test test')
    response.setHeader('test',['a','b','c'])
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

  1. 還可以一起來
const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    response.setHeader('content-type','text/html;charset=utf-8')
    response.setHeader('Server','Node.js')
    response.setHeader('myServer','test test test')
    response.setHeader('test',['a','b','c'])
    response.end('response')
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

響應體

const http=require('http')

const server = http.createServer((request,response)=>{
    //1、設置響應狀態(tài)碼
    // response.statusCode=205
    // response.statusCode=404
    //2、設置響應狀態(tài)描述
    // response.statusMessage='iloveyou';
    //3、響應頭
    // response.setHeader('content-type','text/html;charset=utf-8')
    // response.setHeader('Server','Node.js')
    // response.setHeader('myServer','test test test')
    // response.setHeader('test',['a','b','c'])
    //4、設置響應體
    response.write('how')
    response.write('are')
    response.write('you')
    //一般設置了響應體之后就不設置end了,但是end只能有一個
    response.end()
});

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動....")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js

HTTP響應練習

搭建 HTTP 服務,響應一個 4 行 3 列的表格,并且要求表格有隔行換色效果,且點擊單元格能高亮顯示

const http=require('http')

const server=http.createServer((request,response)=>{
    response.end(`<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            table{
                cursor: pointer;
            }
            td{
                padding: 20px 40px;
            }
            table tr:nth-child(odd){
                 background-color:pink;
            }
            table tr:nth-child(even){
                background-color: skyblue;
            }
        </style>
    </head>
    <body>
      <table border="1" style="border-collapse: collapse">
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
          <tr>
              <td></td>
              <td></td>
              <td></td>
          </tr>
          <tr>
              <td></td>
              <td></td>
              <td></td>
          </tr>
          <tr>
              <td></td>
              <td></td>
              <td></td>
          </tr>
      </table>
      <script>
          let tds=document.querySelectorAll('td')
          tds.forEach(item=>{
              item.οnclick=function (){
                  this.style.backgroundColor='lightcyan'
              }
          })
      </script>
    </body>
    </html>
    `)
})

server.listen(9000,()=>{
    console.log("服務已經(jīng)啟動...")
})

nodejs http 請求頭,Nodejs,前端,http,網(wǎng)絡協(xié)議,網(wǎng)絡,node.js文章來源地址http://www.zghlxwxcb.cn/news/detail-830336.html

到了這里,關于Nodejs基礎6之HTTP模塊的獲取請求行和請求頭、獲取請求體、獲取請求路徑和查詢字符串、http請求練習、設置HTTP響應報文、http響應練習的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • NodeJS入門:常用模塊匯總之http模塊(1)

    NodeJS入門:常用模塊匯總之http模塊(1)

    接收html文件、json數(shù)據(jù)的demo 文件解釋: index.html:主頁面 about.html:\\\"關于我們\\\"頁面 index.js:服務器配置和響應設置 代碼: index.html about.html: 簡單功能測試 index.js: 創(chuàng)建服務器對象、設置端口、服務器響應后返回一個html標簽,讓頁面顯示\\\"good morning\\\" // 引入需要的模塊 const

    2024年04月16日
    瀏覽(51)
  • Nodejs中http 模塊介紹以及服務器相關的概念

    Nodejs中http 模塊介紹以及服務器相關的概念

    什么是客戶端、什么是服務器? 在網(wǎng)絡節(jié)點中,負責消費資源的電腦,叫做 客戶端 負責對外提供網(wǎng)絡資源的電腦,叫做 服務器 http 模塊是 Node.is 官方提供的、用來創(chuàng)建 web 服務器的模塊,通過 http 模塊提供的 http.createServer() 方法,就能方便的把一臺普通的電腦,變成一臺 W

    2024年03月23日
    瀏覽(21)
  • Java獲取請求頭、參數(shù)、路徑、

    request.getReader()和request.getParameter(\\\"key\\\") 方法只能讀取一次,重復讀取會報IO異常 從? ContainerRequestContext? ?對象 獲取請求頭、路徑、參數(shù) ?注意:這里MultivaluedMap和map不同,遍歷的時候也不同,MultivaluedMap?一個key?可以有多個值 ,?map一個key?只對應一個值 舉個例子: 從 Http

    2023年04月23日
    瀏覽(24)
  • Caddy反向代理轉(zhuǎn)發(fā)修改http請求路徑

    Caddy是個非常不錯的開源服務器產(chǎn)品,簡單易用,自帶ssl。只是沒啥詳細的中文文檔,遇到問題只能看官方文檔。 記錄一下使用Caddy轉(zhuǎn)發(fā)http請求的方法。 問題:將http://192.168.1.10:7077/product/*的請求轉(zhuǎn)發(fā)到http://192.168.1.12:7078/*。這里其實是兩個需求,一個是轉(zhuǎn)發(fā)端口,還有個是去

    2024年02月12日
    瀏覽(25)
  • NodeJS 后端通過Http獲取Base64格式數(shù)據(jù)顯示圖片 ②〇

    NodeJS 后端通過Http獲取Base64格式數(shù)據(jù)顯示圖片 ②〇

    Node.js 是一個javascript運行環(huán)境。它讓javascript可以開 發(fā)后端程序 ,實現(xiàn)幾乎其他后端語言實現(xiàn)的所有功能,可以與```PHP、Java、Python、.NET、Ruby等后端語言平起平坐。 Nodejs是基于V8引擎,V8是Google發(fā)布的開源JavaScript引擎,本身就是用于Chrome瀏覽器的JS解釋,但是Node之父 Ryan Dah

    2024年02月16日
    瀏覽(229)
  • python os模塊獲取文件路徑

    1、 # 獲取當前工作目錄的上一級目錄 dir_path = os.path.dirname(os.path.abspath(\\\'.\\\')) 2、獲取當前路徑: 3、獲取當前路徑的文件名: file_name = os.path.basename(os.getcwd()) 獲取當前路徑下所有文件名: file_names = os.listdir(os.getcwd()) 4、字符串正則化 字符串正則化(string normalization)是指將不同

    2024年02月14日
    瀏覽(17)
  • springmvc 獲取項目中的所有請求路徑

    springboot/springmvc 獲取項目中的所有請求路徑 1. 編寫業(yè)務代碼 2. 異常信息 No qualifying bean of type ‘org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping’ available: expected single matching bean but found 3: swagger2ControllerMapping,requestMappingHandlerMapping,controllerEndpointHandlerMapping 原因就

    2024年02月09日
    瀏覽(19)
  • python 模塊requests 發(fā)送 HTTP 請求

    一、簡介 requests 模塊是 python 基于 urllib,采用 Apache2 Licensed 開源協(xié)議的 HTTP 庫。它比 urllib 更加方便,可以節(jié)約我們大量的工作 二、安裝 三、方法 requsts.requst(method, url,headers,cookies,proxies,timeout) method:請求方式;字符串類型 url:請求的地址;字符串類型 params:查詢參數(shù),g

    2024年02月11日
    瀏覽(24)
  • vue請求本地路徑GET http://localhost:8080/.... 404 (Not Found)

    vue請求本地路徑GET http://localhost:8080/.... 404 (Not Found)

    正在學習vue,將遇到的問題當作筆記寫到這里,希望可以幫助有同樣問題的同學 在開發(fā)過程中通過axios請求本地json數(shù)據(jù),首先看一下我的文件結(jié)構(gòu) 報錯 GET http://localhost:8080/public/data/nongyeyuanqu.json 404 (Not Found) 查了很多資料,由于版本不同的問題還是沒有解決 后來看到一篇帖子

    2024年02月13日
    瀏覽(21)
  • 【二、http】go的http基本請求設置(設置查詢參數(shù)、定制請求頭)get和post類似

    結(jié)果: 結(jié)果: 可以看到其中頭部信息已經(jīng)打印出自己增加的部分內(nèi)容

    2024年02月05日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包