理解互連網網域名稱請求實現過程,以及Nodejs的http請求小談

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

前提:在學習開發互連網網站程式前,需要瞭解知道一個用戶端請求,如何能展現成一個炫麗的網頁的。

一、網域名稱請求實現

  這幅圖足以說明一個網域名稱請求的過程了吧

二、伺服器端的處理(Nodejs樣本)

  直接上nodejs代碼

  

 1 var http = require(‘http‘); 2  3 http.createServer(function(req, res) { 4     if (req.method === ‘GET‘) { 5         var html; 6         switch (req.url) { 7             case ‘/‘: 8             case ‘/index.html‘: 9                 html = ‘<html><head><meta charset="utf8"><title>首頁</title></head>‘ +10                     ‘<body>‘ +11                     ‘<h1>歡迎訪問<h1>‘+12                     ‘<a href="add.html">add</a> ‘ +13                     ‘<a href="delete.html">delete</a> ‘ +14                     ‘<a href="update.html">update</a> ‘ +15                     ‘<a href="select.html">select</a> ‘ +16                     ‘</body></html>‘;17                 break;18             case ‘/add.html‘:19                 html = ‘<html><head><title>add</title></head>‘ +20                     ‘<body>‘ +21                     ‘<a href="index.html">index</a>‘ +22                     ‘add‘ +23                     ‘</body></html>‘;24                 break;25             case ‘/delete.html‘:26                 html = ‘<html><head><title>delete</title></head>‘ +27                     ‘<body>‘ +28                     ‘<a href="index.html">index</a>‘ +29                     ‘delete‘ +30                     ‘</body></html>‘;31                 break;32             case ‘/update.html‘:33                 html = ‘<html><head><title>update</title></head>‘ +34                     ‘<body>‘ +35                     ‘<a href="index.html">index</a>‘ +36                     ‘update‘ +37                     ‘</body></html>‘;38                 break;39             case ‘/select.html‘:40                 html = ‘<html><head><title>delete</title></head>‘ +41                     ‘<body>‘ +42                     ‘<a href="index.html">index</a>‘ +43                     ‘delete‘ +44                     ‘</body></html>‘;45                 break;46             default:47                 res.writeHead(‘Content-Type‘, "text/html");48                 res.end(‘404‘);49                 return;50         }51         res.writeHead(‘Content-Type‘, "text/html");52         res.end(html);53     }54 }).listen(7789);55 console.log(‘http://localhost:7789‘);
View Code

這裡可以明確看出

  var http = require(‘http‘);//建立http對象,http是啥?百度、google去吧

  http.createServer(function(request, response) {}).listen(7789)//建立httpServer 伺服器,以及最後面的監聽連接埠

  

  說明:

    前提:伺服器已經使用 node 命令啟動http服務並且監聽連接埠7798,這個比較簡單,將以上代碼存成一個檔案“JSWebsite.js”,存放於D盤下,

    此時從cmd中進入D盤下,運行 node JSWebsite.js 即可,建立http服務,並且監聽所要監聽的連接埠。

    nodejs 使用代碼方式建立了http協議網站的伺服器,以及監聽連接埠,其他web伺服器也類似,IIS可以建立網站,以及IP,連接埠的監聽

    1.當請求到達伺服器時,由於伺服器已存在,對應請求的ip、連接埠的監聽,所以會交給監聽程式處理,這裡交給了,在建立http服務時候的匿名函數  

    http.createServer(function(request, response) {}).listen(7789),即function(request,response){}方法處理。

    2.request 主要是請求攜帶資訊,response主要是要響應的表達資訊

    request,包含了請求的method、URL等諸多資訊。

    response,包含了響應的head、Content-type等諸多資訊

    其實其他的web伺服器,也是響應類似的封裝了以上操作,這個處理過程也是類似的操作

    無外乎:請求→處理→響應

擴充:
  1.大家可能看到以上代碼的switch case處理,非常麻煩,怎麼辦
    大牛走的路多了,給您歸納總結出,路由規則

  2.也看到了手寫html的麻煩。
    這時也會有相應的讀取實際物理地址檔案的操作,將html代碼與邏輯分開,表現與邏輯的分離,MVC、MVVM等等

  3.網站的整體架構就是一團亂
    這時相應的網站架構替您排憂解難,express等等

 

注:NodeJs開發學習目錄

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.