首頁  >  文章  >  web前端  >  讓Express支援async方法分享

讓Express支援async方法分享

小云云
小云云原創
2018-01-26 16:36:291662瀏覽

本文主要介紹了詳解如何讓Express支持async/await,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

隨著Node.js v8 的發布,Node.js 已原生支援async/await 函數,Web 框架Koa 也隨之發布了Koa 2 正式版,支援async/await 中間件,為處理非同步回調帶來了極大的方便。

既然 Koa 2 已經支援 async/await 中間件了,為什麼不直接用 Koa,而還要去改造 Express 讓其支援 async/await 中間件呢?因為Koa 2 正式版發布才不久,而很多老項目用的都還是Express,不可能將其推倒用Koa 重寫,這樣成本太高,但又想用到新語法帶來的便利,那就只能對Express 進行改造了,而且這種改造必須是對業務無侵入的,不然會帶來很多的麻煩。

直接使用 async/await

讓我們先來看看在 Express 中直接使用 async/await 函數的情況。

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 進行改造,直接使用async/await 函數來處理請求,當請求http://127.0.0.1:3000/ 時,發現請求能正常請求,回應也能正常回應。這樣似乎不對 Express 做任何改造也能直接使用 async/await 函數,但如果 async/await 函數裡發生了錯誤能不能被我們的錯誤處理中間件處理呢?現在我們去讀取一個不存在文件,例如將先前讀取的 package.json 換成 age.json 。

app.get('/', async function (req, res, next){
 const data = await readFileAsync('./age.json');
 res.send(data.toString());
});

現在我們去請求 http://127.0.0.1:3000/ 時,發現請求遲遲不能回應,最終會逾時。而在終端機報瞭如下的錯誤:

發現錯誤並沒有被錯誤處理中間件處理,而是拋出了一個unhandledRejection 異常,現在如果我們用try/ catch 來手動捕捉錯誤會是什麼情況呢?

app.get('/', async function (req, res, next){
 try {
  const data = await readFileAsync('./age.json');
  res.send(datas.toString());
 } catch(e) {
  next(e);
 }
});

發現請求被錯誤處理中間件處理了,表示我們手動明確的來捕獲錯誤是可以的,但是如果在每個中間件或請求處理函數裡面加一個try/catch 也太不優雅了,對業務碼有一定的侵入性,程式碼也顯得難看。所以直接使用 async/await 函數的實驗,我們發現對 Express 改造的方向就是能夠接收 async/await 函數裡面拋出的錯誤,又對業務程式碼沒有侵入性。

改造Express

在Express 中有兩種方式來處理路由和中間件,一種是透過Express 創建的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 建立的路由實例,直接在路由實例上新增中間件和處理路由,像下面這樣:

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 }/`);
});

這兩種方法可以混合起來用,現在我們思考一下怎樣才能讓一個形如app.get('/', async function(req, res, next){}) 的函數,讓裡面的async 函數拋出的錯誤能被統一處理呢?要讓錯誤被統一的處理當然要呼叫next(err) 來讓錯誤被傳遞到錯誤處理中間件,又由於async 函數回傳的是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() 這個函數的參數中,若有async 函數,就採用item(req, res, next).then(next).catch(next); 來處理,這樣就能捕獲函數內拋出的錯誤,並傳到錯誤處理中間件裡面去。但這段程式碼有一個明顯的錯誤就是最後呼叫 app.get(),這樣就遞歸了,破壞了 app.get 的功能,也根本處理不了請求,因此還需要繼續改造。

我們之前說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 的源碼,發現這樣破壞了app.get() 這個方法,因為app.get() 不僅能用來處理路由,而且還能用來獲取應用的配置,在Express 中對應的源碼如下:

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;
 };
});

所以在改造時,我們也需要對app.get 做特殊處理。在實際的應用程式中我們不僅有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 }/`);
});

現在就改造完了,我們只需要加一小段程式碼,就可以直接用async function 作為handler 處理請求,對業務也毫無侵入性,拋出的錯誤也能傳遞到錯誤處理中間件。

相關推薦:

NodeJs透過async和await處理異步的方法

##Node.js中如何使用async函數

ES6之async+await同步/非同步方案詳解

以上是讓Express支援async方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn