Node.js筆記(0001)---connect模組

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   cti   

首先來看這一部分代碼

 1 /** 2  * Created by bsn on 14-7-1. 3  */ 4 var connect = require(‘connect‘); 5  6 var app = connect(); 7 function hello(req, res, next) { 8     console.log(req.url); 9     res.end(‘hello bsn‘);10     next();11 }12 13 function helloAgain(req, res) {14     console.log(‘hello again‘);15     res.end(‘hello again‘);16 }17 18 app.use(hello)19     .listen(3000);

如果請求http://localhost:3000

這一部分代碼的輸出是什麼(控制台和瀏覽器中)?

首先想到的第一個答案可能是

瀏覽器:hello bsnhello again

控制台:/

然而實際上輸出的是:

瀏覽器:hello bsn

控制台:/

           /favicon.ico

 

為什嗎?

關於控制台的輸出結果是因為請求了兩次,除了我們手動觸發的那一次,還包括瀏覽器自動去請求表徵圖的那一次,這一點自然好理解

那關於瀏覽器為什麼輸出的不是hello bsnhello again呢?

因為在第一次請求的時候,在hello方法處理中使用了res.end(...);即resonse已經結束,後面的響應就失效,如果hello方法是這樣寫的:

1 function hello(req, res, next) {2     console.log(req.url);3     res.write(‘hello bsn‘);4     next();5 }

那結果就是瀏覽器輸出hello bsnhello again

關於這個模組自己知識剛開始接觸,後續自己也會深入去學習

聯繫我們

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