koa-compose<\/strong><\/div>\nKoa-compose \u662f\u4e00\u4e2a Koa \u4e2d\u95f4\u4ef6\u5de5\u5177\uff0cKoa \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Node.js \u7f51\u7edc\u6846\u67b6\u3002Koa-compose \u5141\u8bb8\u4f60\u5c06\u591a\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\u7ec4\u5408\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u548c\u91cd\u7528\u4e2d\u95f4\u4ef6\u3002<\/p>\n
\u5728 Koa \u4e2d\uff0c\u4e2d\u95f4\u4ef6\u51fd\u6570\u662f\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8c03\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u4f20\u5165\u7684 HTTP \u8bf7\u6c42\u5e76\u751f\u6210\u54cd\u5e94\u3002\u4e2d\u95f4\u4ef6\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u4efb\u52a1\uff0c\u4f8b\u5982\u89e3\u6790\u8bf7\u6c42\u4e3b\u4f53\u3001\u9a8c\u8bc1\u8bf7\u6c42\u53c2\u6570\u6216\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/p>\n
\u4e2d\u95f4\u4ef6\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n
\/\/ sendHandle.js\r\nconst sendHandle = () => {\r\n \/\/ \u5904\u7406\u8bf7\u6c42\u6210\u529f\u65b9\u6cd5\r\n const render = ctx => {\r\n return (data, msg = '\u8bf7\u6c42\u6210\u529f') => {\r\n ctx.set('Content-Type', 'application\/json');\r\n ctx.body = {\r\n code: '000001',\r\n data,\r\n msg\r\n }\r\n }\r\n }\r\n \/\/ \u5904\u7406\u8bf7\u6c42\u5931\u8d25\u65b9\u6cd5\r\n const renderError = ctx => {\r\n return (code, msg = '\u8bf7\u6c42\u5931\u8d25') => {\r\n ctx.set('Content-Type', 'application\/json');\r\n ctx.body = {\r\n code,\r\n data: null,\r\n msg\r\n }\r\n }\r\n }\r\n return async (ctx, next) => {\r\n ctx.send = render(ctx);\r\n ctx.sendError = renderError(ctx);\r\n await next();\r\n }\r\n}\r\nmodule.exports = sendHandle;<\/pre>\n\u7136\u540e\u5728app.js \u4e2d\u5f15\u7528\uff0c\u5982\u56fe<\/p>\n
\u4e0a\u9762\u4e2d\u95f4\u4ef6\u7684\u4f5c\u7528\u662f\u5904\u7406\u8bf7\u6c42\u7684\u7ed3\u679c\u3002<\/p>\n
\u8be6\u7ec6\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\uff0ccodeniu\/niu-box \uff0c\u8fd9\u662f\u4e00\u4e2a koa \u5c0f\u9879\u76ee\u3002<\/p>\n
\u6d0b\u8471\u6a21\u578b<\/strong><\/div>\n\u6d0b\u8471\u6a21\u578b\u662f\u4e00\u79cd\u7528\u4e8e\u89e3\u91ca\u4e2d\u95f4\u4ef6\u7684\u67b6\u6784\u6a21\u578b\u3002\u5b83\u63cf\u8ff0\u4e86\u4e2d\u95f4\u4ef6\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u4ee5\u53ca\u4e2d\u95f4\u4ef6\u5982\u4f55\u5728\u5904\u7406\u8bf7\u6c42\u548c\u751f\u6210\u54cd\u5e94\u65f6\u4e0e\u5176\u4ed6\u4e2d\u95f4\u4ef6\u534f\u540c\u5de5\u4f5c\u3002<\/p>\n
\u5728\u6d0b\u8471\u6a21\u578b\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u88ab\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u5305\u88c5\u5728\u5916\u5c42\u7684\u51fd\u6570\u3002\u6bcf\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\u90fd\u53ef\u4ee5\u5728\u63a5\u6536\u5230\u8bf7\u6c42\u65f6\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u5185\u5c42\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\u3002\u5f53\u5185\u5c42\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\u5b8c\u6210\u5de5\u4f5c\u5e76\u8fd4\u56de\u54cd\u5e94\u65f6\uff0c\u5916\u5c42\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\u53ef\u4ee5\u518d\u6b21\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u7136\u540e\u5c06\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n
\u6e90\u7801\u89e3\u6790<\/strong><\/div>\n'use strict'\r\n\/**\r\n * Expose compositor.\r\n *\/\r\nmodule.exports = compose\r\n\/**\r\n * Compose `middleware` returning\r\n * a fully valid middleware comprised\r\n * of all those which are passed.\r\n *\r\n * @param {Array} middleware\r\n * @return {Function}\r\n * @api public\r\n *\/\r\nfunction compose (middleware) {\r\n if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')\r\n for (const fn of middleware) {\r\n if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')\r\n }\r\n \/**\r\n * @param {Object} context\r\n * @return {Promise}\r\n * @api public\r\n *\/\r\n return function (context, next) {\r\n \/\/ last called middleware #\r\n let index = -1\r\n return dispatch(0)\r\n function dispatch (i) {\r\n if (i <= index) return Promise.reject(new Error('next() called multiple times'))\r\n index = i\r\n let fn = middleware[i]\r\n if (i === middleware.length) fn = next\r\n if (!fn) return Promise.resolve()\r\n try {\r\n return Promise.resolve(fn(context, dispatch.bind(null, i + 1)))\r\n } catch (err) {\r\n return Promise.reject(err)\r\n }\r\n }\r\n }\r\n}<\/pre>\ncompose() \u51fd\u6570\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6570\u7ec4\uff0c\u5b83\u5305\u542b\u4e86\u8981\u7ec4\u5408\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\u3002\u9996\u5148\uff0c\u4ee3\u7801\u4f1a\u68c0\u67e5\u4e2d\u95f4\u4ef6\u6570\u7ec4\u662f\u5426\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u90fd\u662f\u51fd\u6570\u3002\u5982\u679c\u4e2d\u95f4\u4ef6\u6570\u7ec4\u4e0d\u5408\u6cd5\uff0c\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n
\u7136\u540e\uff0ccompose() \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1acontext \u548c next\u3002context \u5bf9\u8c61\u5305\u542b\u4e86\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4f8b\u5982\u8bf7\u6c42\u8def\u5f84\u3001\u8bf7\u6c42\u53c2\u6570\u7b49\u3002next \u51fd\u6570\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u5f53\u524d\u4e2d\u95f4\u4ef6\u51fd\u6570\u5b8c\u6210\u5de5\u4f5c\u540e\u8c03\u7528\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\u3002<\/p>\n
\u53d8\u91cf index\uff0c\u7528\u4e8e\u8bb0\u5f55\u6700\u540e\u4e00\u4e2a\u88ab\u8c03\u7528\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\u7684\u7f16\u53f7\u3002<\/p>\n
\u5728\u6bcf\u6b21\u8c03\u7528\u4e2d\u95f4\u4ef6\u51fd\u6570\u4e4b\u524d\uff0c\u90fd\u4f1a\u68c0\u67e5\u5f53\u524d\u4e2d\u95f4\u4ef6\u51fd\u6570\u7684\u7f16\u53f7\u662f\u5426\u5c0f\u4e8e\u7b49\u4e8e index \u53d8\u91cf\u3002\u5982\u679c\u662f\uff0c\u5c31\u610f\u5473\u7740 next() \u51fd\u6570\u88ab\u8c03\u7528\u4e86\u591a\u6b21\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u3002\u7136\u540e\u4f1a\u66f4\u65b0 index \u53d8\u91cf\uff0c\u5e76\u83b7\u53d6\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\u3002<\/p>\n
\u5982\u679c\u5f53\u524d\u4e2d\u95f4\u4ef6\u51fd\u6570\u662f\u6700\u540e\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\uff0c\u5c31\u4f1a\u5c06 next \u51fd\u6570\u8d4b\u503c\u7ed9\u5f53\u524d\u4e2d\u95f4\u4ef6\u51fd\u6570\u3002\u5982\u679c\u6ca1\u6709\u66f4\u591a\u7684\u4e2d\u95f4\u4ef6\u51fd\u6570\uff0c\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5df2\u5b8c\u6210\u7684 Promise \u5bf9\u8c61\u3002<\/p>\n
\u6700\u540e\uff0c\u8c03\u7528\u5f53\u524d\u4e2d\u95f4\u4ef6\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\u3002\u5982\u679c\u5728\u8c03\u7528\u8fc7\u7a0b\u4e2d\u53d1\u751f\u9519\u8bef\u5219\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\nkoa-compose \u4f7f\u7528\u9012\u5f52\u548cPromise\u6765\u5b9e\u73b0\u591a\u4e2a\u4e2d\u95f4\u4ef6\u7684\u94fe\u5f0f\u8c03\u7528\uff0cPromise \u5f88\u597d\u7684\u7b80\u5316\u4e86\u5f02\u6b65\u6d41\u7a0b\uff0c\u5e76\u4e14\u80fd\u591f\u8ba9\u4f60\u4f7f\u7528 try-catch \u8bed\u53e5\u6355\u83b7\u5f02\u6b65\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Koa-compose \u662f\u4e00\u4e2a Koa \u4e2d\u95f4\u4ef6\u5de5\u5177\uff0cKoa \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Node.js \u7f51\u7edc\u6846\u67b6\u3002Koa-c […]<\/p>\n","protected":false},"author":1479,"featured_media":85409,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260478"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260478\/revisions"}],"predecessor-version":[{"id":260481,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260478\/revisions\/260481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85409"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}