隨著 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 Handlerapp.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ò)誤處理中間件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注