<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                我們先大致看看NodeJS提供了哪些和網絡操作有關的API。這里并不逐一介紹每個API的使用方法,官方文檔已經做得很好了。 ## HTTP > **官方文檔:?**[](http://nodejs.org/api/http.html)[http://nodejs.org/api/http.html](http://nodejs.org/api/http.html) 'http'模塊提供兩種使用方式: * 作為服務端使用時,創建一個HTTP服務器,監聽HTTP客戶端請求并返回響應。 * 作為客戶端使用時,發起一個HTTP客戶端請求,獲取服務端響應。 首先我們來看看服務端模式下如何工作。如開門紅中的例子所示,首先需要使用`.createServer`方法創建一個服務器,然后調用`.listen`方法監聽端口。之后,每當來了一個客戶端請求,創建服務器時傳入的回調函數就被調用一次。可以看出,這是一種事件機制。 HTTP請求本質上是一個數據流,由請求頭(headers)和請求體(body)組成。例如以下是一個完整的HTTP請求數據內容。 ~~~ POST / HTTP/1.1 User-Agent: curl/7.26.0 Host: localhost Accept: */* Content-Length: 11 Content-Type: application/x-www-form-urlencoded Hello World ~~~ 可以看到,空行之上是請求頭,之下是請求體。HTTP請求在發送給服務器時,可以認為是按照從頭到尾的順序一個字節一個字節地以數據流方式發送的。而`http`模塊創建的HTTP服務器在接收到完整的請求頭后,就會調用回調函數。在回調函數中,除了可以使用`request`對象訪問請求頭數據外,還能把`request`對象當作一個只讀數據流來訪問請求體數據。以下是一個例子。 ~~~ http.createServer(function (request, response) { var body = []; console.log(request.method); console.log(request.headers); request.on('data', function (chunk) { body.push(chunk); }); request.on('end', function () { body = Buffer.concat(body); console.log(body.toString()); }); }).listen(80); ------------------------------------ POST { 'user-agent': 'curl/7.26.0', host: 'localhost', accept: '*/*', 'content-length': '11', 'content-type': 'application/x-www-form-urlencoded' } Hello World ~~~ HTTP響應本質上也是一個數據流,同樣由響應頭(headers)和響應體(body)組成。例如以下是一個完整的HTTP請求數據內容。 ~~~ HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 11 Date: Tue, 05 Nov 2013 05:31:38 GMT Connection: keep-alive Hello World ~~~ 在回調函數中,除了可以使用`response`對象來寫入響應頭數據外,還能把`response`對象當作一個只寫數據流來寫入響應體數據。例如在以下例子中,服務端原樣將客戶端請求的請求體數據返回給客戶端。 ~~~ http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/plain' }); request.on('data', function (chunk) { response.write(chunk); }); request.on('end', function () { response.end(); }); }).listen(80); ~~~ 接下來我們看看客戶端模式下如何工作。為了發起一個客戶端HTTP請求,我們需要指定目標服務器的位置并發送請求頭和請求體,以下示例演示了具體做法。 ~~~ var options = { hostname: 'www.example.com', port: 80, path: '/upload', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; var request = http.request(options, function (response) {}); request.write('Hello World'); request.end(); ~~~ 可以看到,`.request`方法創建了一個客戶端,并指定請求目標和請求頭數據。之后,就可以把`request`對象當作一個只寫數據流來寫入請求體數據和結束請求。另外,由于HTTP請求中`GET`請求是最常見的一種,并且不需要請求體,因此`http`模塊也提供了以下便捷API。 ~~~ http.get('http://www.example.com/', function (response) {}); ~~~ 當客戶端發送請求并接收到完整的服務端響應頭時,就會調用回調函數。在回調函數中,除了可以使用`response`對象訪問響應頭數據外,還能把`response`對象當作一個只讀數據流來訪問響應體數據。以下是一個例子。 ~~~ http.get('http://www.example.com/', function (response) { var body = []; console.log(response.statusCode); console.log(response.headers); response.on('data', function (chunk) { body.push(chunk); }); response.on('end', function () { body = Buffer.concat(body); console.log(body.toString()); }); }); ------------------------------------ 200 { 'content-type': 'text/html', server: 'Apache', 'content-length': '801', date: 'Tue, 05 Nov 2013 06:08:41 GMT', connection: 'keep-alive' } <!DOCTYPE html> ... ~~~ ## HTTPS > **官方文檔:?**[](http://nodejs.org/api/https.html)[http://nodejs.org/api/https.html](http://nodejs.org/api/https.html) `https`模塊與`http`模塊極為類似,區別在于`https`模塊需要額外處理SSL證書。 在服務端模式下,創建一個HTTPS服務器的示例如下。 ~~~ var options = { key: fs.readFileSync('./ssl/default.key'), cert: fs.readFileSync('./ssl/default.cer') }; var server = https.createServer(options, function (request, response) { // ... }); ~~~ 可以看到,與創建HTTP服務器相比,多了一個`options`對象,通過`key`和`cert`字段指定了HTTPS服務器使用的私鑰和公鑰。 另外,NodeJS支持SNI技術,可以根據HTTPS客戶端請求使用的域名動態使用不同的證書,因此同一個HTTPS服務器可以使用多個域名提供服務。接著上例,可以使用以下方法為HTTPS服務器添加多組證書。 ~~~ server.addContext('foo.com', { key: fs.readFileSync('./ssl/foo.com.key'), cert: fs.readFileSync('./ssl/foo.com.cer') }); server.addContext('bar.com', { key: fs.readFileSync('./ssl/bar.com.key'), cert: fs.readFileSync('./ssl/bar.com.cer') }); ~~~ 在客戶端模式下,發起一個HTTPS客戶端請求與`http`模塊幾乎相同,示例如下。 ~~~ var options = { hostname: 'www.example.com', port: 443, path: '/', method: 'GET' }; var request = https.request(options, function (response) {}); request.end(); ~~~ 但如果目標服務器使用的SSL證書是自制的,不是從頒發機構購買的,默認情況下`https`模塊會拒絕連接,提示說有證書安全問題。在`options`里加入`rejectUnauthorized: false`字段可以禁用對證書有效性的檢查,從而允許`https`模塊請求開發環境下使用自制證書的HTTPS服務器。 ## URL > **官方文檔:?**[](http://nodejs.org/api/url.html)[http://nodejs.org/api/url.html](http://nodejs.org/api/url.html) 處理HTTP請求時`url`模塊使用率超高,因為該模塊允許解析URL、生成URL,以及拼接URL。首先我們來看看一個完整的URL的各組成部分。 ~~~ href ----------------------------------------------------------------- host path --------------- ---------------------------- http: // user:pass @ host.com : 8080 /p/a/t/h ?query=string #hash ----- --------- -------- ---- -------- ------------- ----- protocol auth hostname port pathname search hash ------------ query ~~~ 我們可以使用`.parse`方法來將一個URL字符串轉換為URL對象,示例如下。 ~~~ url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string#hash'); /* => { protocol: 'http:', auth: 'user:pass', host: 'host.com:8080', port: '8080', hostname: 'host.com', hash: '#hash', search: '?query=string', query: 'query=string', pathname: '/p/a/t/h', path: '/p/a/t/h?query=string', href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' } */ ~~~ 傳給`.parse`方法的不一定要是一個完整的URL,例如在HTTP服務器回調函數中,`request.url`不包含協議頭和域名,但同樣可以用`.parse`方法解析。 ~~~ http.createServer(function (request, response) { var tmp = request.url; // => "/foo/bar?a=b" url.parse(tmp); /* => { protocol: null, slashes: null, auth: null, host: null, port: null, hostname: null, hash: null, search: '?a=b', query: 'a=b', pathname: '/foo/bar', path: '/foo/bar?a=b', href: '/foo/bar?a=b' } */ }).listen(80); ~~~ `.parse`方法還支持第二個和第三個布爾類型可選參數。第二個參數等于`true`時,該方法返回的URL對象中,`query`字段不再是一個字符串,而是一個經過`querystring`模塊轉換后的參數對象。第三個參數等于`true`時,該方法可以正確解析不帶協議頭的URL,例如`//www.example.com/foo/bar`。 反過來,`format`方法允許將一個URL對象轉換為URL字符串,示例如下。 ~~~ url.format({ protocol: 'http:', host: 'www.example.com', pathname: '/p/a/t/h', search: 'query=string' }); /* => 'http://www.example.com/p/a/t/h?query=string' */ ~~~ 另外,`.resolve`方法可以用于拼接URL,示例如下。 ~~~ url.resolve('http://www.example.com/foo/bar', '../baz'); /* => http://www.example.com/baz */ ~~~ ## Query String > **官方文檔:?**[](http://nodejs.org/api/querystring.html)[http://nodejs.org/api/querystring.html](http://nodejs.org/api/querystring.html) `querystring`模塊用于實現URL參數字符串與參數對象的互相轉換,示例如下。 ~~~ querystring.parse('foo=bar&baz=qux&baz=quux&corge'); /* => { foo: 'bar', baz: ['qux', 'quux'], corge: '' } */ querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' }); /* => 'foo=bar&baz=qux&baz=quux&corge=' */ ~~~ ## Zlib > **官方文檔:?**[](http://nodejs.org/api/zlib.html)[http://nodejs.org/api/zlib.html](http://nodejs.org/api/zlib.html) `zlib`模塊提供了數據壓縮和解壓的功能。當我們處理HTTP請求和響應時,可能需要用到這個模塊。 首先我們看一個使用`zlib`模塊壓縮HTTP響應體數據的例子。這個例子中,判斷了客戶端是否支持gzip,并在支持的情況下使用`zlib`模塊返回gzip之后的響應體數據。 ~~~ http.createServer(function (request, response) { var i = 1024, data = ''; while (i--) { data += '.'; } if ((request.headers['accept-encoding'] || '').indexOf('gzip') !== -1) { zlib.gzip(data, function (err, data) { response.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Encoding': 'gzip' }); response.end(data); }); } else { response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end(data); } }).listen(80); ~~~ 接著我們看一個使用`zlib`模塊解壓HTTP響應體數據的例子。這個例子中,判斷了服務端響應是否使用gzip壓縮,并在壓縮的情況下使用`zlib`模塊解壓響應體數據。 ~~~ var options = { hostname: 'www.example.com', port: 80, path: '/', method: 'GET', headers: { 'Accept-Encoding': 'gzip, deflate' } }; http.request(options, function (response) { var body = []; response.on('data', function (chunk) { body.push(chunk); }); response.on('end', function () { body = Buffer.concat(body); if (response.headers['content-encoding'] === 'gzip') { zlib.gunzip(body, function (err, data) { console.log(data.toString()); }); } else { console.log(data.toString()); } }); }).end(); ~~~ ## Net > **官方文檔:?**[](http://nodejs.org/api/net.html)[http://nodejs.org/api/net.html](http://nodejs.org/api/net.html) `net`模塊可用于創建Socket服務器或Socket客戶端。由于Socket在前端領域的使用范圍還不是很廣,這里先不涉及到WebSocket的介紹,僅僅簡單演示一下如何從Socket層面來實現HTTP請求和響應。 首先我們來看一個使用Socket搭建一個很不嚴謹的HTTP服務器的例子。這個HTTP服務器不管收到啥請求,都固定返回相同的響應。 ~~~ net.createServer(function (conn) { conn.on('data', function (data) { conn.write([ 'HTTP/1.1 200 OK', 'Content-Type: text/plain', 'Content-Length: 11', '', 'Hello World' ].join('\n')); }); }).listen(80); ~~~ 接著我們來看一個使用Socket發起HTTP客戶端請求的例子。這個例子中,Socket客戶端在建立連接后發送了一個HTTP GET請求,并通過`data`事件監聽函數來獲取服務器響應。 ~~~ var options = { port: 80, host: 'www.example.com' }; var client = net.connect(options, function () { client.write([ 'GET / HTTP/1.1', 'User-Agent: curl/7.26.0', 'Host: www.baidu.com', 'Accept: */*', '', '' ].join('\n')); }); client.on('data', function (data) { console.log(data.toString()); client.end(); }); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看