小編給大家分享一下如何讓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 重寫,這樣成本太高,但又想用到新語法帶來的便利,那就只能對(duì) Express 進(jìn)行改造了,而且這種改造必須是對(duì)業(yè)務(wù)無侵入的,不然會(huì)帶來很多的麻煩。
直接使用 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 }/`);
});上面是沒有對(duì) Express 進(jìn)行改造,直接使用 async/await 函數(shù)來處理請(qǐng)求,當(dāng)請(qǐng)求 http://127.0.0.1:3000/ 時(shí),發(fā)現(xiàn)請(qǐng)求能正常請(qǐng)求,響應(yīng)也能正常響應(yīng)。這樣似乎不對(duì) 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)フ?qǐng)求 http://127.0.0.1:3000/ 時(shí),發(fā)現(xiàn)請(qǐng)求遲遲不能響應(yīng),最終會(huì)超時(shí)。而在終端報(bào)了如下的錯(cuò)誤:

發(fā)現(xiàn)錯(cuò)誤并沒有被錯(cuò)誤處理中間件處理,而是拋出了一個(gè) unhandledRejection 異常,現(xiàn)在如果我們用 try/catch 來手動(dòng)捕獲錯(cuò)誤會(huì)是什么情況呢?
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)請(qǐng)求被錯(cuò)誤處理中間件處理了,說明我們手動(dòng)顯式的來捕獲錯(cuò)誤是可以的,但是如果在每個(gè)中間件或請(qǐng)求處理函數(shù)里面加一個(gè) try/catch 也太不優(yōu)雅了,對(duì)業(yè)務(wù)代碼有一定的侵入性,代碼也顯得難看。所以通過直接使用 async/await 函數(shù)的實(shí)驗(yàn),我們發(fā)現(xiàn)對(duì) Express 改造的方向就是能夠接收 async/await 函數(shù)里面拋出的錯(cuò)誤,又對(duì)業(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 的功能,也根本處理不了請(qǐng)求,因此還需要繼續(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)
}像上面這樣改造之后似乎一切都能正常工作了,能正常處理請(qǐng)求了。但通過查看 Express 的源碼,發(fā)現(xiàn)這樣破壞了 app.get() 這個(gè)方法,因?yàn)?app.get() 不僅能用來處理路由,而且還能用來獲取應(yīng)用的配置,在 Express 中對(duì)應(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í),我們也需要對(duì) app.get 做特殊處理。在實(shí)際的應(yīng)用中我們不僅有 get 請(qǐng)求,還有 post、put 和 delete 等請(qǐng)求,所以我們最終改造的代碼如下:
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 處理請(qǐng)求,對(duì)業(yè)務(wù)也毫無侵入性,拋出的錯(cuò)誤也能傳遞到錯(cuò)誤處理中間件。
以上是“如何讓Express支持async/await”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁名稱:如何讓Express支持async/await-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article6/eojog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、App開發(fā)、ChatGPT、營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容