<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # http Node.js開發的目的就是為了用JavaScript編寫Web服務器程序。因為JavaScript實際上已經統治了瀏覽器端的腳本,其優勢就是有世界上數量最多的前端開發人員。如果已經掌握了JavaScript前端開發,再學習一下如何將JavaScript應用在后端開發,就是名副其實的_全棧_了。 ## HTTP協議 要理解Web服務器程序的工作原理,首先,我們要對HTTP協議有基本的了解。如果你對HTTP協議不太熟悉,先看一看[HTTP協議簡介](http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432011939547478fd5482deb47b08716557cc99764e0000)。 ## HTTP服務器 要開發HTTP服務器程序,從頭處理TCP連接,解析HTTP是不現實的。這些工作實際上已經由Node.js自帶的`http`模塊完成了。應用程序并不直接和HTTP協議打交道,而是操作`http`模塊提供的`request`和`response`對象。 `request`對象封裝了HTTP請求,我們調用`request`對象的屬性和方法就可以拿到所有HTTP請求的信息; `response`對象封裝了HTTP響應,我們操作`response`對象的方法,就可以把HTTP響應返回給瀏覽器。 用Node.js實現一個HTTP服務器程序非常簡單。我們來實現一個最簡單的Web程序`hello.js`,它對于所有請求,都返回`Hello world!`: ``` 'use strict'; // 導入http模塊: var http = require('http'); // 創建http server,并傳入回調函數: var server = http.createServer(function (request, response) { // 回調函數接收request和response對象, // 獲得HTTP請求的method和url: console.log(request.method + ': ' + request.url); // 將HTTP響應200寫入response, 同時設置Content-Type: text/html: response.writeHead(200, {'Content-Type': 'text/html'}); // 將HTTP響應的HTML內容寫入response: response.end('<h1>Hello world!</h1>'); }); // 讓服務器監聽8080端口: server.listen(8080); console.log('Server is running at http://127.0.0.1:8080/'); ``` 在命令提示符下運行該程序,可以看到以下輸出: ``` $ node hello.js Server is running at http://127.0.0.1:8080/ ``` 不要關閉命令提示符,直接打開瀏覽器輸入`http://localhost:8080`,即可看到服務器響應的內容: ![http-hello-sample](img/l16.png) 同時,在命令提示符窗口,可以看到程序打印的請求信息: ``` GET: / GET: /favicon.ico ``` 這就是我們編寫的第一個HTTP服務器程序! ## 文件服務器 讓我們繼續擴展一下上面的Web程序。我們可以設定一個目錄,然后讓Web程序變成一個文件服務器。要實現這一點,我們只需要解析`request.url`中的路徑,然后在本地找到對應的文件,把文件內容發送出去就可以了。 解析URL需要用到Node.js提供的`url`模塊,它使用起來非常簡單,通過`parse()`將一個字符串解析為一個`Url`對象: ``` 'use strict'; var url = require('url'); console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=string#hash')); ``` 結果如下: ``` Url { protocol: 'http:', slashes: true, auth: 'user:pass', host: 'host.com:8080', port: '8080', hostname: 'host.com', hash: '#hash', search: '?query=string', query: 'query=string', pathname: '/path/to/file', path: '/path/to/file?query=string', href: 'http://user:pass@host.com:8080/path/to/file?query=string#hash' } ``` 處理本地文件目錄需要使用Node.js提供的`path`模塊,它可以方便地構造目錄: ``` 'use strict'; var path = require('path'); // 解析當前目錄: var workDir = path.resolve('.'); // '/Users/michael' // 組合完整的文件路徑:當前目錄+'pub'+'index.html': var filePath = path.join(workDir, 'pub', 'index.html'); // '/Users/michael/pub/index.html' ``` 使用`path`模塊可以正確處理操作系統相關的文件路徑。在Windows系統下,返回的路徑類似于`C:\Users\michael\static\index.html`,這樣,我們就不關心怎么拼接路徑了。 最后,我們實現一個文件服務器`file_server.js`: ``` 'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require('http'); // 從命令行參數獲取root目錄,默認是當前目錄: var root = path.resolve(process.argv[2] || '.'); console.log('Static root dir: ' + root); // 創建服務器: var server = http.createServer(function (request, response) { // 獲得URL的path,類似 '/css/bootstrap.css': var pathname = url.parse(request.url).pathname; // 獲得對應的本地文件路徑,類似 '/srv/www/css/bootstrap.css': var filepath = path.join(root, pathname); // 獲取文件狀態: fs.stat(filepath, function (err, stats) { if (!err && stats.isFile()) { // 沒有出錯并且文件存在: console.log('200 ' + request.url); // 發送200響應: response.writeHead(200); // 將文件流導向response: fs.createReadStream(filepath).pipe(response); } else { // 出錯了或者文件不存在: console.log('404 ' + request.url); // 發送404響應: response.writeHead(404); response.end('404 Not Found'); } }); }); server.listen(8080); console.log('Server is running at http://127.0.0.1:8080/'); ``` 沒有必要手動讀取文件內容。由于`response`對象本身是一個`Writable Stream`,直接用`pipe()`方法就實現了自動讀取文件內容并輸出到HTTP響應。 在命令行運行`node file_server.js /path/to/dir`,把`/path/to/dir`改成你本地的一個有效的目錄,然后在瀏覽器中輸入`http://localhost:8080/index.html`: ![http-index-page](img/l17.png) 只要當前目錄下存在文件`index.html`,服務器就可以把文件內容發送給瀏覽器。觀察控制臺輸出: ``` 200 /index.html 200 /css/uikit.min.css 200 /js/jquery.min.js 200 /fonts/fontawesome-webfont.woff2 ``` 第一個請求是瀏覽器請求`index.html`頁面,后續請求是瀏覽器解析HTML后發送的其它資源請求。 ## 練習 在瀏覽器輸入`http://localhost:8080/`時,會返回404,原因是程序識別出HTTP請求的不是文件,而是目錄。請修改`file_server.js`,如果遇到請求的路徑是目錄,則自動在目錄下依次搜索`index.html`、`default.html`,如果找到了,就返回HTML文件的內容。 ## 參考源碼 [http服務器代碼](https://github.com/michaelliao/learn-javascript/tree/master/samples/node/http)(含靜態網站)
                  <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>

                              哎呀哎呀视频在线观看