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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[success] # Express 中間件 ~~~ 1.中間件(middleware)就是處理HTTP請求的函數。它最大的特點就是, 一個中間件處理完,再傳遞給下一個中間件 2.中間件有三個參數分別是依次為request對象(代表HTTP請求)、 response對象(代表HTTP回應),next回調函數(代表下一個中間件)。 3.簡單說中間件就像一個過濾器可以,給每個url在請求前加一個操作,比如 請求時間,請求IP之類的。 ~~~ >[danger] ##### use -- 注冊調用中間件 ~~~ 1.當我們寫好了中間件方法,可以使用use 進行調用中間件 2.app.use的作用是將一個中間件綁定到應用中,參數path是一個路徑前 綴,用于限定中間件的作用范圍,所有以該前綴開始的請求路徑均是中間 件的作用范圍,不考慮http的請求方法 ~~~ >[danger] ##### use 和 method ~~~ 1.路由規則是app.use(path,router)定義的,router代表一個由e xpress.Router()創建的對象,在路由對象中可定義多個路由規則。可是如 果我們的路由只有一條規則時,可直接接一個回調作為簡寫,也可直接使 用app.get或app.post方法。即 2.use 和 method 都可以調用中間件,兩者區別當一個路徑有多個匹配規則 時,使用app.use,否則使用相應的app.method(get、post) 3.use 不受請求狀態的限制,但method 會被指定請求狀態 ~~~ >[danger] ##### 中間件函數 -- next 講解 ~~~ 1.簡單的案例模仿next()方法 2.這個案例涉及到了,函數隊列,回調函數,閉包,遞歸 3.在express內部,有一個函數的數組,暫時叫這個數組**tasks**,每來一 個請求express內部會依次執行這個數組中的函數 ~~~ ~~~ var http = require('http'); function express(){ var funcs = []; var expr = function(req,res){ var i = 0; function next(){ var task = funcs[i++]; if(!task) return; task(req,res,next); } next(); } expr.use=function(f){ funcs.push(f); } return expr; } var app = express(); app.use(function(req,res,next){ console.log('haha'); next(); }); app.use(function(req,res,next){ console.log('hehe'); next(); }); app.use(function(req,res){ res.end("there is nothing happened"); }); // createServer 函數參數 function(req,res),因為app 返回的是expr ,就變 // 相的相當于expr(req,res) http.createServer(app).listen('3000', function(){ console.log('Express server listening on port 3000'); }); ~~~ >[success] # 中間案例 ~~~ 1.中間如果不啟用next 的方法,就不會講內容傳遞給下一個中間件,整 個程序就會被hang住。簡單的說next方法的作用就是把請求傳遞到下一個 中間件 2.next 中可以填寫參數,如果填寫的參數是route 可以直接跳轉到下一個路由 3.可以定義方法后,用數組順序決定中間件運行順序。 4.中間件錯誤處理參數 ~~~ >[danger] ##### 簡單的使用 -- 定一個請求時間的中間 ~~~ const express = require('express'); const app = express(); let visitTime = (req, res, next)=>{ let dt = new Date(); console.log(Number(dt)); // 給下一個中間件app.get next(); }; // use 可以接受全類型的 請求,我們封裝的一個方法用來記錄請求時間的 app.use(visitTime); app.get('/', function (req, res, next) { res.send("111") }); app.listen('3000',function () { console.log("11") }); ~~~ >[danger] ##### 簡單案例 -- next 跳轉到指定路由 ~~~ 1.沒加route 參數的話,跳轉acb,加了跳轉到hello ~~~ ~~~ const express = require('express'); const app = express(); app.get('/abc',(req,res,next)=>{ console.log(1); // 跳轉到下一個路由 //next('route'); next() },(req,res)=>{ console.log(2); res.send('abc'); }); app.get('/abc',(req,res)=>{ console.log(3); res.send('hello'); }); app.listen('3000',function () { console.log("11") }); ~~~ >[danger] ##### 簡單案例 -- 用數組決定順序 ~~~ const express = require('express'); const app = express(); var cb0 = function (req, res, next) { console.log('CB0'); next(); } var cb1 = function (req, res, next) { console.log('CB1'); next(); } var cb2 = function (req, res) { res.send('Hello from C!'); } app.get('/example', [cb0, cb1, cb2]); // app.use('/example', [cb0, cb1, cb2]); app.listen('3000',function () { console.log("11") }); ~~~ >[danger] ##### 簡單案例 -- 處理錯誤500/404 中間件 ~~~ app.use(function (err, req, res, next) { console.error(err.stack) res.status(500).send('Something broke!') }) ~~~
                  <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>

                              哎呀哎呀视频在线观看