你需要安裝一些東西來(lái)創(chuàng)建、使用和測(cè)試 Express 中間件。首先需要 Node 和 NPM。為確保已經(jīng)安裝,可以運(yùn)行:
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、慶云網(wǎng)站維護(hù)、網(wǎng)站推廣。npm -v && node -v
你應(yīng)該看到已安裝的 Node 和 NPM 版本。如果出現(xiàn)錯(cuò)誤,則需要安裝 Node。所有例子都應(yīng)在 Node ver 8+ 和NPM ver 5+ 下使用。
本文使用了 Express 4.x 版。這很重要,因?yàn)閺?3.x 版到 4.x 版有重大的更改。
Express中間件:基礎(chǔ)首先我們使用 Express 最基本的內(nèi)置中間件。創(chuàng)建一個(gè)新項(xiàng)目并 npm 初始化它…
npm init npm install express --save Create server.js and paste the following code: const express = require('express'); const app = express(); app.get('/', (req, res, next) => { res.send('Welcome Home'); }); app.listen(3000);中間件解決什么問(wèn)題?為什么要用它?
假設(shè)你在 web 網(wǎng)絡(luò)服務(wù)器上正在使用 Node.js 和 Express 運(yùn)行Web應(yīng)用程序。在此應(yīng)用中,你需要登錄的某些頁(yè)面。
當(dāng) Web 服務(wù)器收到數(shù)據(jù)請(qǐng)求時(shí),Express 將為你提供一個(gè)請(qǐng)求對(duì)象,其中包含有關(guān)用戶(hù)及其所請(qǐng)求數(shù)據(jù)的信息。 Express 還使你可以訪(fǎng)問(wèn)響應(yīng)對(duì)象,可以在Web服務(wù)器響應(yīng)用戶(hù)之前對(duì)其進(jìn)行修改。這些對(duì)象通??s短為 req,res。
中間件函數(shù)是使用相關(guān)信息修改 req 和 res 對(duì)象的理想場(chǎng)所。例如用戶(hù)登錄后,你可以從數(shù)據(jù)庫(kù)中獲取其用戶(hù)詳細(xì)信息,然后將這些詳細(xì)信息存儲(chǔ)在 res.user
中。
async function userMiddleware (req, res, next) { try { const userData = await getUserData(req.params.id); //see app.get below if(userData) { req.user = userData; next(); } } catch(error) { res.status(500).send(error.message); //replace with proper error handling } }
如果出現(xiàn)錯(cuò)誤,并且你不想執(zhí)行其他代碼,則不要調(diào)用該函數(shù)。請(qǐng)記住在這種情況下要發(fā)送響應(yīng),否則客戶(hù)端將會(huì)等待響應(yīng)直到超時(shí)。
var app = express(); //your normal route Handlers app.get('/user/:id', userMiddleware, userController);中間件鏈
你可以在中間件數(shù)組中或著通過(guò)使用多個(gè) app.use
調(diào)用來(lái)鏈接中間件:
app.use(middlewareA); app.use(middlewareB); app.get('/', [middlewareC, middlewareD], handler);
Express 收到請(qǐng)求后,與請(qǐng)求相匹配的每個(gè)中間件都將會(huì)按照初始化的順序運(yùn)行,直到有終止操作為止。
因此,如果發(fā)生錯(cuò)誤,則將按順序調(diào)用所有用于處理錯(cuò)誤的中間件,直到其中一個(gè)不再調(diào)用 next() 函數(shù)調(diào)用為止。
Express中間件的類(lèi)型路由器級(jí)中間件,例如:router.use內(nèi)置中間件,例如:express.static,express.json,express.urlencoded錯(cuò)誤處理中間件,例如:app.use(err,req,res,next)第三方中間件,例如:bodyparser、cookieparser路由器級(jí)中間件express.Router 使用 express.Router 類(lèi)創(chuàng)建模塊化的、可安裝的路由處理。路由實(shí)例是一個(gè)完整的中間件和路由系統(tǒng)。
你可以用中間件進(jìn)行日志記錄、身份驗(yàn)證等操作。如下所示,以記錄用戶(hù)的最新活動(dòng)并解析身份驗(yàn)證標(biāo)頭,用它確定當(dāng)前登錄的用戶(hù)并將其添加到 Request 對(duì)象。該函數(shù)在程序每次收到請(qǐng)求時(shí)執(zhí)行。如果有錯(cuò)誤,它會(huì)僅結(jié)束響應(yīng),而不會(huì)調(diào)用后續(xù)的中間件或路由處理。var router = express.Router() //Load router-level middleware by using the router.use() and router.METHOD() functions. //The following example creates a router as a module, loads a middleware function in it, // defines some routes, and mounts the router module on a path in the main app. var express = require(‘express’); var router = express.Router(); // a middleware function with no mount path. This code is executed for // every request to the router // logging async function logMiddleware (req, res, next) { try { console.log(req.user.id, new Date()); next(); } catch() { res.status(500).send(error.message); } } // authentication async function checkAuthentication(req, res, next) => { // check header or url parameters or post parameters for token const token = req.body.token || req.query.token || req.headers['x-access-token'] || req.headers['authorization']; if (token) { try { // verifies secret req.decoded = await jwt.verify(token, config.secret) let checkUser = await authenticateTokenHelper.getUserDetail(req); // if everything is good, save to request for use in other routes if (checkUser) { req.user = req.decoded next() } else { return res.status(403).json({ message: responseMessage.noAuthorized }) } } catch (err) { return res.status(401).json({ message: responseMessage.invalidToken }) } } else { // if there is no token return res.status(400).json({ message: responseMessage.invalidRequest }) } } router.use(logMiddleware); router.get('/user, checkAuthentication, handler);內(nèi)置中間件
Express 有以下內(nèi)置的中間件功能:
express.static
提供靜態(tài)資源,例如 HTML 文件,圖像等。express.json
負(fù)載解析用 JSON 傳入的請(qǐng)求。express.urlencoded
解析傳入的用 URL 編碼的有效載荷請(qǐng)求。錯(cuò)誤處理中間件錯(cuò)誤處理中間件始終采用四個(gè)參數(shù)(err,req,res,next)。你必須通過(guò)提供四個(gè)參數(shù)來(lái)將其標(biāo)識(shí)為錯(cuò)誤處理中間件函數(shù)。即使你不需要使用 next 對(duì)象,也必須指定。否則 next 對(duì)象將被解釋為常規(guī)中間件,并將會(huì)無(wú)法處理錯(cuò)誤?;竞灻缦滤荆?/p>
app.use(function (err, req, res, next) { console.error(err.stack) res.status(500).send('Something broke!') })
例1:
app.get('/users', (req, res, next) => { next(new Error('I am passing you an error!')); }); app.use((err, req, res, next) => { console.log(err); if(!res.headersSent){ res.status(500).send(err.message); } });
在這種情況下,管道末端的錯(cuò)誤處理中間件將會(huì)處理該錯(cuò)誤。你可能還會(huì)注意到,我檢查了 res.headersSent
屬性。這只是檢查響應(yīng)是否已經(jīng)將標(biāo)頭發(fā)送到客戶(hù)端。如果還沒(méi)有,它將向客戶(hù)端發(fā)送 HTTP 500 狀態(tài)和錯(cuò)誤消息。
例2:
你還可以鏈接錯(cuò)誤處理中間件。通常以不同的方式處理不同類(lèi)型的錯(cuò)誤:
app.get('/users, (req, res, next) => { let err = new Error('I couldn\\'t find it.'); err.httpStatusCode = 404; next(err); }); app.get('/user, (req, res, next) => { let err = new Error('I\\'m sorry, you can\\'t do that, Dave.'); err.httpStatusCode = 304; next(err); }); app.use((err, req, res, next) => { // handles not found errors if (err.httpStatusCode === 404) { res.status(400).render('NotFound'); } // handles unauthorized errors else if(err.httpStatusCode === 304){ res.status(304).render('Unauthorized'); } // catch all else if (!res.headersSent) { res.status(err.httpStatusCode || 500).render('UnknownError'); } next(err); });在這種情況下,中間件檢查是否拋出了 404(not found)錯(cuò)誤。如果是,它將渲染 “NotFound” 模板頁(yè)面,然后將錯(cuò)誤傳遞到中間件中的下一項(xiàng)。下一個(gè)中間件檢查是否拋出了 304(unauthorized)錯(cuò)誤。如果是,它將渲染“Unauthorized”頁(yè)面,并將錯(cuò)誤傳遞到管道中的下一個(gè)中間件。最后,“catch all” 錯(cuò)誤處理僅記錄錯(cuò)誤,如果未發(fā)送響應(yīng),它將發(fā)送錯(cuò)誤的 httpStatusCode(如果未提供則發(fā)送 HTTP 500 狀態(tài))并渲染 “UnknownError” 模板。第三方級(jí)別的中間件
在某些情況下,我們將向后端添加一些額外的功能。先安裝 Node.js 模塊獲取所需的功能,然后在應(yīng)用級(jí)別或路由器級(jí)別將其加載到你的應(yīng)用中。
示例:當(dāng) body-parser 處理 Content-Type 請(qǐng)求標(biāo)頭時(shí),所有中間件都將使用解析的正文填充 req.body
屬性。
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) app.post('/save',(req,res)=>{ res.json({ "status":true, "payload":req.body }) } app.listen(3000,(req,res)=>{ console.log('server running on port') })總結(jié)
中間件功能是一種非常好的方式,可以對(duì)每個(gè)請(qǐng)求或針對(duì)特定路由的每個(gè)請(qǐng)求運(yùn)行代碼,并對(duì)請(qǐng)求或響應(yīng)數(shù)據(jù)采取措施。中間件是現(xiàn)代 Web 服務(wù)器的重要組成部分,并且非常有用。
英文原文地址:https://www.thirdrocktechkno.com/blog/how-Node-JS-middleware-works/
為了保證的可讀性,本文采用意譯而非直譯。
更多編程相關(guān)知識(shí),請(qǐng)?jiān)L問(wèn):編程入門(mén)?。?
本文名稱(chēng):淺談Node.js中間件的工作原理
當(dāng)前地址:http://m.rwnh.cn/article46/cgeoeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、電子商務(wù)、Google、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容