当前位置:Gxlcms > JavaScript > 让Express支持async方法分享

让Express支持async方法分享

时间:2021-07-01 10:21:17 帮助过:7人阅读

本文主要介绍了详解如何让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 函数的情况。

  1. const express = require('express');
  2. const app = express();
  3. const { promisify } = require('util');
  4. const { readFile } = require('fs');
  5. const readFileAsync = promisify(readFile);
  6. app.get('/', async function (req, res, next){
  7. const data = await readFileAsync('./package.json');
  8. res.send(data.toString());
  9. });
  10. // Error Handler
  11. app.use(function (err, req, res, next){
  12. console.error('Error:', err);
  13. res.status(500).send('Service Error');
  14. });
  15. app.listen(3000, '127.0.0.1', function (){
  16. console.log(`Server running at http://${this.address().address }:${this.address().port }/`);
  17. });

上面是没有对 Express 进行改造,直接使用 async/await 函数来处理请求,当请求 http://127.0.0.1:3000/ 时,发现请求能正常请求,响应也能正常响应。这样似乎不对 Express 做任何改造也能直接使用 async/await 函数,但如果 async/await 函数里发生了错误能不能被我们的错误处理中间件处理呢?现在我们去读取一个不存在文件,例如将之前读取的 package.json 换成 age.json 。

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

现在我们去请求 http://127.0.0.1:3000/ 时,发现请求迟迟不能响应,最终会超时。而在终端报了如下的错误:

发现错误并没有被错误处理中间件处理,而是抛出了一个 unhandledRejection 异常,现在如果我们用 try/catch 来手动捕获错误会是什么情况呢?

  1. app.get('/', async function (req, res, next){
  2. try {
  3. const data = await readFileAsync('./age.json');
  4. res.send(datas.toString());
  5. } catch(e) {
  6. next(e);
  7. }
  8. });

发现请求被错误处理中间件处理了,说明我们手动显式的来捕获错误是可以的,但是如果在每个中间件或请求处理函数里面加一个 try/catch 也太不优雅了,对业务代码有一定的侵入性,代码也显得难看。所以通过直接使用 async/await 函数的实验,我们发现对 Express 改造的方向就是能够接收 async/await 函数里面抛出的错误,又对业务代码没有侵入性。

改造 Express

在 Express 中有两种方式来处理路由和中间件,一种是通过 Express 创建的 app,直接在 app 上添加中间件和处理路由,像下面这样:

  1. const express = require('express');
  2. const app = express();
  3. app.use(function (req, res, next){
  4. next();
  5. });
  6. app.get('/', function (req, res, next){
  7. res.send('hello, world');
  8. });
  9. app.post('/', function (req, res, next){
  10. res.send('hello, world');
  11. });
  12. app.listen(3000, '127.0.0.1', function (){
  13. console.log(`Server running at http://${this.address().address }:${this.address().port }/`);
  14. });

另外一种是通过 Express 的 Router 创建的路由实例,直接在路由实例上添加中间件和处理路由,像下面这样:

  1. const express = require('express');
  2. const app = express();
  3. const router = new express.Router();
  4. app.use(router);
  5. router.get('/', function (req, res, next){
  6. res.send('hello, world');
  7. });
  8. router.post('/', function (req, res, next){
  9. res.send('hello, world');
  10. });
  11. app.listen(3000, '127.0.0.1', function (){
  12. console.log(`Server running at http://${this.address().address }:${this.address().port }/`);
  13. });

这两种方法可以混合起来用,现在我们思考一下怎样才能让一个形如 app.get('/', async function(req, res, next){}) 的函数,让里面的 async 函数抛出的错误能被统一处理呢?要让错误被统一的处理当然要调用 next(err) 来让错误被传递到错误处理中间件,又由于 async 函数返回的是 Promise,所以肯定是形如这样的 asyncFn().then().catch(function(err){ next(err) }) ,所以按这样改造一下就有如下的代码:

  1. app.get = function (...data){
  2. const params = [];
  3. for (let item of data) {
  4. if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') {
  5. params.push(item);
  6. continue;
  7. }
  8. const handle = function (...data){
  9. const [ req, res, next ] = data;
  10. item(req, res, next).then(next).catch(next);
  11. };
  12. params.push(handle);
  13. }
  14. app.get(...params)
  15. }

上面的这段代码中,我们判断 app.get() 这个函数的参数中,若有 async 函数,就采用 item(req, res, next).then(next).catch(next); 来处理,这样就能捕获函数内抛出的错误,并传到错误处理中间件里面去。但是这段代码有一个明显的错误就是最后调用 app.get(),这样就递归了,破坏了 app.get 的功能,也根本处理不了请求,因此还需要继续改造。

我们之前说 Express 两种处理路由和中间件的方式可以混用,那么我们就混用这两种方式来避免递归,代码如下:

  1. const express = require('express');
  2. const app = express();
  3. const router = new express.Router();
  4. app.use(router);
  5. app.get = function (...data){
  6. const params = [];
  7. for (let item of data) {
  8. if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') {
  9. params.push(item);
  10. continue;
  11. }
  12. const handle = function (...data){
  13. const [ req, res, next ] = data;
  14. item(req, res, next).then(next).catch(next);
  15. };
  16. params.push(handle);
  17. }
  18. router.get(...params)
  19. }

像上面这样改造之后似乎一切都能正常工作了,能正常处理请求了。但通过查看 Express 的源码,发现这样破坏了 app.get() 这个方法,因为 app.get() 不仅能用来处理路由,而且还能用来获取应用的配置,在 Express 中对应的源码如下:

  1. methods.forEach(function(method){
  2. app[method] = function(path){
  3. if (method === 'get' && arguments.length === 1) {
  4. // app.get(setting)
  5. return this.set(path);
  6. }
  7. this.lazyrouter();
  8. var route = this._router.route(path);
  9. route[method].apply(route, slice.call(arguments, 1));
  10. return this;
  11. };
  12. });

所以在改造时,我们也需要对 app.get 做特殊处理。在实际的应用中我们不仅有 get 请求,还有 post、put 和 delete 等请求,所以我们最终改造的代码如下:

  1. const { promisify } = require('util');
  2. const { readFile } = require('fs');
  3. const readFileAsync = promisify(readFile);
  4. const express = require('express');
  5. const app = express();
  6. const router = new express.Router();
  7. const methods = [ 'get', 'post', 'put', 'delete' ];
  8. app.use(router);
  9. for (let method of methods) {
  10. app[method] = function (...data){
  11. if (method === 'get' && data.length === 1) return app.set(data[0]);
  12. const params = [];
  13. for (let item of data) {
  14. if (Object.prototype.toString.call(item) !== '[object AsyncFunction]') {
  15. params.push(item);
  16. continue;
  17. }
  18. const handle = function (...data){
  19. const [ req, res, next ] = data;
  20. item(req, res, next).then(next).catch(next);
  21. };
  22. params.push(handle);
  23. }
  24. router[method](...params);
  25. };
  26. }
  27. app.get('/', async function (req, res, next){
  28. const data = await readFileAsync('./package.json');
  29. res.send(data.toString());
  30. });
  31. app.post('/', async function (req, res, next){
  32. const data = await readFileAsync('./age.json');
  33. res.send(data.toString());
  34. });
  35. router.use(function (err, req, res, next){
  36. console.error('Error:', err);
  37. res.status(500).send('Service Error');
  38. });
  39. app.listen(3000, '127.0.0.1', function (){
  40. console.log(`Server running at http://${this.address().address }:${this.address().port }/`);
  41. });

现在就改造完了,我们只需要加一小段代码,就可以直接用 async function 作为 handler 处理请求,对业务也毫无侵入性,抛出的错误也能传递到错误处理中间件。

相关推荐:

NodeJs通过async和await处理异步的方法

Node.js中如何使用async函数

ES6之async+await同步/异步方案详解

以上就是让Express支持async方法分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行