詳解如何讓Express支持async/await
隨著 Node.js v8 的發(fā)布,Node.js 已原生支持 async/await 函數(shù),Web 框架 Koa 也隨之發(fā)布了 Koa 2 正式版,支持 async/await 中間件,為處理異步回調(diào)帶來了極大的方便。
既然 Koa 2 已經(jīng)支持 async/await 中間件了,為什么不直接用 Koa,而還要去改造 Express 讓其支持 async/await 中間件呢?因?yàn)?Koa 2 正式版發(fā)布才不久,而很多老項(xiàng)目用的都還是 Express,不可能將其推倒用 Koa 重寫,這樣成本太高,但又想用到新語法帶來的便利,那就只能對 Express 進(jìn)行改造了,而且這種改造必須是對業(yè)務(wù)無侵入的,不然會帶來很多的麻煩。
直接使用 async/await
讓我們先來看下在 Express 中直接使用 async/await 函數(shù)的情況。
const express = require('express'); const app = express(); const { promisify } = require('util'); const { readFile } = require('fs'); const readFileAsync = promisify(readFile); app.get('/', async function (req, res, next){ const data = await readFileAsync('./package.json'); res.send(data.toString()); }); // Error Handler app.use(function (err, req, res, next){ console.error('Error:', err); res.status(500).send('Service Error'); }); app.listen(3000, '127.0.0.1', function (){ console.log(`Server running at http://${this.address().address }:${this.address().port }/`); });
上面是沒有對 Express 進(jìn)行改造,直接使用 async/await 函數(shù)來處理請求,當(dāng)請求 http://127.0.0.1:3000/ 時(shí),發(fā)現(xiàn)請求能正常請求,響應(yīng)也能正常響應(yīng)。這樣似乎不對 Express 做任何改造也能直接使用 async/await 函數(shù),但如果 async/await 函數(shù)里發(fā)生了錯(cuò)誤能不能被我們的錯(cuò)誤處理中間件處理呢?現(xiàn)在我們?nèi)プx取一個(gè)不存在文件,例如將之前讀取的 package.json 換成 age.json 。
app.get('/', async function (req, res, next){ const data = await readFileAsync('./age.json'); res.send(data.toString()); });
現(xiàn)在我們?nèi)フ埱?http://127.0.0.1:3000/ 時(shí),發(fā)現(xiàn)請求遲遲不能響應(yīng),最終會超時(shí)。而在終端報(bào)了如下的錯(cuò)誤:
發(fā)現(xiàn)錯(cuò)誤并沒有被錯(cuò)誤處理中間件處理,而是拋出了一個(gè) unhandledRejection 異常,現(xiàn)在如果我們用 try/catch 來手動捕獲錯(cuò)誤會是什么情況呢?
app.get('/', async function (req, res, next){ try { const data = await readFileAsync('./age.json'); res.send(datas.toString()); } catch(e) { next(e); } });
發(fā)現(xiàn)請求被錯(cuò)誤處理中間件處理了,說明我們手動顯式的來捕獲錯(cuò)誤是可以的,但是如果在每個(gè)中間件或請求處理函數(shù)里面加一個(gè) try/catch 也太不優(yōu)雅了,對業(yè)務(wù)代碼有一定的侵入性,代碼也顯得難看。所以通過直接使用 async/await 函數(shù)的實(shí)驗(yàn),我們發(fā)現(xiàn)對 Express 改造的方向就是能夠接收 async/await 函數(shù)里面拋出的錯(cuò)誤,又對業(yè)務(wù)代碼沒有侵入性。
改造 Express
在 Express 中有兩種方式來處理路由和中間件,一種是通過 Express 創(chuàng)建的 app,直接在 app 上添加中間件和處理路由,像下面這樣:
const express = require('express'); const app = express(); app.use(function (req, res, next){ next(); }); app.get('/', function (req, res, next){ res.send('hello, world'); }); app.post('/', function (req, res, next){ res.send('hello, world'); }); app.listen(3000, '127.0.0.1', function (){ console.log(`Server running at http://${this.address().address }:${this.address().port }/`); });
另外一種是通過 Express 的 Router 創(chuàng)建的路由實(shí)例,直接在路由實(shí)例上添加中間件和處理路由,像下面這樣:
const express = require('express'); const app = express(); const router = new express.Router(); app.use(router); router.get('/', function (req, res, next){ res.send('hello, world'); }); router.post('/', function (req, res, next){ res.send('hello, world'); }); app.listen(3000, '127.0.0.1', function (){ console.log(`Server running at http://${this.address().address }:${this.address().port }/`); });
這兩種方法可以混合起來用,現(xiàn)在我們思考一下怎樣才能讓一個(gè)形如 app.get('/', async function(req, res, next){}) 的函數(shù),讓里面的 async 函數(shù)拋出的錯(cuò)誤能被統(tǒng)一處理呢?要讓錯(cuò)誤被統(tǒng)一的處理當(dāng)然要調(diào)用 next(err) 來讓錯(cuò)誤被傳遞到錯(cuò)誤處理中間件,又由于 async 函數(shù)返回的是 Promise,所以肯定是形如這樣的 asyncFn().then().catch(function(err){ next(err) }) ,所以按這樣改造一下就有如下的代碼:
app.get = function (...data){ const params = []; for (let item of data) { if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') { params.push(item); continue; } const handle = function (...data){ const [ req, res, next ] = data; item(req, res, next).then(next).catch(next); }; params.push(handle); } app.get(...params) }
上面的這段代碼中,我們判斷 app.get() 這個(gè)函數(shù)的參數(shù)中,若有 async 函數(shù),就采用 item(req, res, next).then(next).catch(next); 來處理,這樣就能捕獲函數(shù)內(nèi)拋出的錯(cuò)誤,并傳到錯(cuò)誤處理中間件里面去。但是這段代碼有一個(gè)明顯的錯(cuò)誤就是最后調(diào)用 app.get(),這樣就遞歸了,破壞了 app.get 的功能,也根本處理不了請求,因此還需要繼續(xù)改造。
我們之前說 Express 兩種處理路由和中間件的方式可以混用,那么我們就混用這兩種方式來避免遞歸,代碼如下:
const express = require('express'); const app = express(); const router = new express.Router(); app.use(router); app.get = function (...data){ const params = []; for (let item of data) { if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') { params.push(item); continue; } const handle = function (...data){ const [ req, res, next ] = data; item(req, res, next).then(next).catch(next); }; params.push(handle); } router.get(...params) }
像上面這樣改造之后似乎一切都能正常工作了,能正常處理請求了。但通過查看 Express 的源碼,發(fā)現(xiàn)這樣破壞了 app.get() 這個(gè)方法,因?yàn)?app.get() 不僅能用來處理路由,而且還能用來獲取應(yīng)用的配置,在 Express 中對應(yīng)的源碼如下:
methods.forEach(function(method){ app[method] = function(path){ if (method === 'get' && arguments.length === 1) { // app.get(setting) return this.set(path); } this.lazyrouter(); var route = this._router.route(path); route[method].apply(route, slice.call(arguments, 1)); return this; }; });
所以在改造時(shí),我們也需要對 app.get 做特殊處理。在實(shí)際的應(yīng)用中我們不僅有 get 請求,還有 post、put 和 delete 等請求,所以我們最終改造的代碼如下:
const { promisify } = require('util'); const { readFile } = require('fs'); const readFileAsync = promisify(readFile); const express = require('express'); const app = express(); const router = new express.Router(); const methods = [ 'get', 'post', 'put', 'delete' ]; app.use(router); for (let method of methods) { app[method] = function (...data){ if (method === 'get' && data.length === 1) return app.set(data[0]); const params = []; for (let item of data) { if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') { params.push(item); continue; } const handle = function (...data){ const [ req, res, next ] = data; item(req, res, next).then(next).catch(next); }; params.push(handle); } router[method](...params); }; } app.get('/', async function (req, res, next){ const data = await readFileAsync('./package.json'); res.send(data.toString()); }); app.post('/', async function (req, res, next){ const data = await readFileAsync('./age.json'); res.send(data.toString()); }); router.use(function (err, req, res, next){ console.error('Error:', err); res.status(500).send('Service Error'); }); app.listen(3000, '127.0.0.1', function (){ console.log(`Server running at http://${this.address().address }:${this.address().port }/`); });
現(xiàn)在就改造完了,我們只需要加一小段代碼,就可以直接用 async function 作為 handler 處理請求,對業(yè)務(wù)也毫無侵入性,拋出的錯(cuò)誤也能傳遞到錯(cuò)誤處理中間件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
node.js中的buffer.toString方法使用說明
這篇文章主要介紹了node.js中的buffer.toString方法使用說明,本文介紹了buffer.toString的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12Nodejs監(jiān)控事件循環(huán)異常示例詳解
這篇文章主要給大家介紹了關(guān)于Nodejs監(jiān)控事件循環(huán)異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Nodejs具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09淺析nodejs實(shí)現(xiàn)Websocket的數(shù)據(jù)接收與發(fā)送
WebSocket是HTML5開始提供的一種瀏覽器與服務(wù)器間進(jìn)行全雙工通訊的網(wǎng)絡(luò)技術(shù),本文給大家介紹nodejs實(shí)現(xiàn)websocket的數(shù)據(jù)庫接收與發(fā)送,小伙伴們一起學(xué)習(xí)吧2015-11-11使用Node.js配合Nginx實(shí)現(xiàn)高負(fù)載網(wǎng)絡(luò)
這篇文章主要介紹了使用Node.js配合Nginx實(shí)現(xiàn)高負(fù)載網(wǎng)絡(luò),Node的異步加上Nginx的反向代理在性能上實(shí)在是給力!需要的朋友可以參考下2015-06-06nodejs開發(fā)一個(gè)最簡單的web服務(wù)器實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于nodejs開發(fā)一個(gè)最簡單的web服務(wù)器實(shí)例內(nèi)容,有需要的朋友們可以參考下。2020-01-01Node.js和Vue的安裝與配置超詳細(xì)步驟(推薦)
使用VUE前端框架開發(fā),需要安裝Node.js和Vue.js,這篇文章主要給大家介紹了關(guān)于Node.js和Vue的安裝與配置超詳細(xì)步驟的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01