{"id":2379,"date":"2020-02-01T18:37:11","date_gmt":"2020-02-01T10:37:11","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=2379"},"modified":"2026-08-18T12:35:23","modified_gmt":"2026-08-18T04:35:23","slug":"koa2-%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e8%bf%9b%e9%98%b6%e4%b8%89-%e4%b8%ad%e9%97%b4%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2379","title":{"rendered":"koa2 \u4ece\u5165\u95e8\u5230\u8fdb\u9636(\u4e09)-\u4e2d\u95f4\u4ef6"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u4e0a\u4e00\u7bc7\u8bb2\u4e86 Koa \u8def\u7531\u3001GET \u4f20\u503c\u548c\u52a8\u6001\u8def\u7531\u3002\u8fd9\u4e00\u7bc7\u770b <strong style=\"color: #4870ac;\">\u4e2d\u95f4\u4ef6<\/strong>\uff0c\u4ee5\u53ca Koa \u7279\u6709\u7684 <strong style=\"color: #4870ac;\">\u6d0b\u8471\u6a21\u578b<\/strong>\uff1a\u8bf7\u6c42\u4e00\u5c42\u5c42\u8d70\u8fdb\u53bb\uff0c\u54cd\u5e94\u518d\u4e00\u5c42\u5c42\u8d70\u51fa\u6765\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e2d\u95f4\u4ef6\u5c31\u662f\u5339\u914d\u8def\u7531\u4e4b\u524d\u6216\u5339\u914d\u5b8c\u6210\u4e4b\u540e\u8981\u505a\u7684\u90a3\u4e32\u64cd\u4f5c\u3002Express \u91cc\u4e2d\u95f4\u4ef6\u80fd\u62ff\u5230 req\u3001res \u548c next\uff1bKoa \u7c7b\u4f3c\uff0c\u53ea\u662f\u4e0a\u4e0b\u6587\u6536\u8fdb <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ctx<\/code>\uff0c\u5411\u4e0b\u8d70\u8981 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">await next()<\/code>\u3002\u4e0d\u8c03\u7528 next\uff0c\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u5339\u914d\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u4e2d\u95f4\u4ef6\u80fd\u505a\u4ec0\u4e48<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u6267\u884c\u4efb\u4f55\u4ee3\u7801\uff1b<\/section>\n<\/li>\n<li>\n<section>\u4fee\u6539\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61\uff1b<\/section>\n<\/li>\n<li>\n<section>\u7ec8\u7ed3\u8bf7\u6c42-\u54cd\u5e94\u5faa\u73af\uff1b<\/section>\n<\/li>\n<li>\n<section>\u8c03\u7528\u5806\u6808\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u3002<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">Koa \u5e94\u7528\u91cc\u5e38\u89c1\u56db\u7c7b\uff1a<strong style=\"color: #4870ac;\">\u5e94\u7528\u7ea7<\/strong>\u3001<strong style=\"color: #4870ac;\">\u8def\u7531\u7ea7<\/strong>\u3001<strong style=\"color: #4870ac;\">\u9519\u8bef\u5904\u7406<\/strong>\u3001<strong style=\"color: #4870ac;\">\u7b2c\u4e09\u65b9<\/strong>\u3002\u7b2c\u4e09\u65b9\u4e2d\u95f4\u4ef6\u540e\u9762\u7528\u5230\u5177\u4f53\u6a21\u5757\u518d\u5c55\u5f00\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7c7b\u578b<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u6302\u8f7d\u65b9\u5f0f<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5178\u578b\u7528\u9014<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5e94\u7528\u7ea7<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">app.use(async (ctx, next) =&gt; {})<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5339\u914d\u4efb\u610f\u8def\u7531\uff0c\u65e5\u5fd7\u3001\u9274\u6743\u3001\u516c\u5171\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8def\u7531\u7ea7<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">router.get(&#8216;\/&#8217;, async (ctx, next) =&gt; {})<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u540c\u4e00\u8def\u5f84\u4e0a\u5148\u5904\u7406\u518d next \u5230\u4e0b\u4e00\u4e2a\u5904\u7406\u5668<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u9519\u8bef\u5904\u7406<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">await next() \u4e4b\u540e\u770b ctx.status<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7edf\u4e00 404 \/ \u5f02\u5e38\u9875<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7b2c\u4e09\u65b9<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">app.use(\u67d0\u6a21\u5757())<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">bodyparser\u3001static\u3001session \u7b49<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u5e94\u7528\u7ea7\u4e2d\u95f4\u4ef6<\/h2>\n<p style=\"margin: 1em 0px;\">\u4e0d\u5199\u8def\u5f84\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">app.use<\/code> \u4f1a\u5339\u914d\u4efb\u610f\u8def\u7531\uff0c\u800c\u4e14\u6700\u5148\u6267\u884c\u3002\u5982\u679c\u4e0d\u5199 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">await next()<\/code>\uff0c\u5339\u914d\u5230\u8fd9\u91cc\u5c31\u505c\uff0c\u4e0d\u4f1a\u518d\u5f80\u4e0b\u8d70\u8def\u7531\u3002\u539f\u6587\u628a\u8fd9\u53e5\u8bdd\u5199\u53cd\u4e86\uff1a\u5199\u4e86 next \u624d\u4f1a\u7ee7\u7eed\uff0c\u4e0d\u5199\u624d\u4f1a\u7ec8\u6b62\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u5f15\u5165 koa\u6a21\u5757\nvar Koa = require('koa');\nvar Router = require('koa-router');\n\n\/\/\u5b9e\u4f8b\u5316\nvar app = new Koa();\nvar router = new Router();\n\n\/\/\u5339\u914d\u4efb\u4f55\u8def\u7531\uff0c\u5982\u679c\u4e0d\u5199next\uff0c\u8fd9\u4e2a\u8def\u7531\u88ab\u5339\u914d\u5230\u4e86\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u5411\u4e0b\u5339\u914d\napp.use(async (ctx, next) => {\n    console.log(\"\u6211\u662f\u4e00\u4e2a\u4e2d\u95f4\u4ef6\");\n    \/\/ \u5f53\u524d\u8def\u7531\u5339\u914d\u5b8c\u6210\u4ee5\u540e\u7ee7\u7eed\u5411\u4e0b\u5339\u914d\n    await next();\n});\n\n\/\/\u914d\u7f6e\u8def\u7531\nrouter.get('\/', async (ctx) => {\n    ctx.body = \"\u9996\u9875\";\n});\nrouter.get('\/news', async (ctx) => {\n    ctx.body = \"\u65b0\u95fb\u5217\u9875\u9762\";\n});\n\n\/\/\u542f\u52a8\u8def\u7531\napp.use(router.routes());\napp.use(router.allowedMethods());\n\napp.listen(3000);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8bbf\u95ee\u4efb\u610f\u5df2\u914d\u7f6e\u8def\u7531\u65f6\uff0c\u63a7\u5236\u53f0\u4f1a\u5148\u6253\u51fa\u300c\u6211\u662f\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u300d\uff0c\u518d\u8fdb\u5165\u5bf9\u5e94\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.get<\/code>\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u8def\u7531\u7ea7\u4e2d\u95f4\u4ef6<\/h2>\n<p style=\"margin: 1em 0px;\">\u540c\u4e00\u4e2a\u8def\u5f84\u53ef\u4ee5\u6302\u4e24\u6761 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.get('\/')<\/code>\uff1a\u7b2c\u4e00\u6761\u6253\u65e5\u5fd7\u5e76 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">await next()<\/code>\uff0c\u7b2c\u4e8c\u6761\u624d\u7ed9\u9875\u9762\u5199 body\u3002\u7b2c\u4e00\u6761\u4e0d\u5199 next\uff0c\u5c31\u4e0d\u4f1a\u8fdb\u5165\u7b2c\u4e8c\u6761\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u5f15\u5165 koa\u6a21\u5757\nvar Koa = require('koa');\nvar Router = require('koa-router');\n\n\/\/\u5b9e\u4f8b\u5316\nvar app = new Koa();\nvar router = new Router();\n\n\/\/\u914d\u7f6e\u8def\u7531\nrouter.get('\/', async (ctx, next) => {\n    console.log(\"\u63a7\u5236\u53f0\u6253\u5370\");\n    \/\/ \u5f53\u524d\u8def\u7531\u5339\u914d\u5b8c\u6210\u4ee5\u540e\u7ee7\u7eed\u5411\u4e0b\u5339\u914d\n    await next();\n});\nrouter.get('\/', async (ctx) => {\n    ctx.body = \"\u9996\u9875\";\n});\nrouter.get('\/news', async (ctx) => {\n    ctx.body = \"\u65b0\u95fb\u5217\u9875\u9762\";\n});\n\n\/\/\u542f\u52a8\u8def\u7531\napp.use(router.routes());\napp.use(router.allowedMethods());\n\napp.listen(3000);<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<\/h2>\n<p style=\"margin: 1em 0px;\">\u9875\u9762\u54cd\u5e94\u4f1a\u5e26 status\u3002Koa \u9ed8\u8ba4\u6ca1\u5339\u914d\u5230\u8def\u7531\u65f6 status \u4ecd\u662f 404\u3002\u628a\u5224\u65ad\u5199\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">await next()<\/code> <strong style=\"color: #4870ac;\">\u4e4b\u540e<\/strong>\uff0c\u7b49\u4e0b\u6e38\u8def\u7531\u8dd1\u5b8c\u518d\u770b\u7ed3\u679c\uff1a\u5339\u914d\u5230 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">\/news<\/code> \u5c31\u6b63\u5e38\u663e\u793a\uff1b\u5c11\u5199\u4e00\u4e2a s \u53d8\u6210 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">\/new<\/code>\uff0c\u5c31\u53ef\u4ee5\u7ed9\u524d\u7aef\u8fd4\u56de\u300c404 \u9875\u9762\u300d\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u5f15\u5165 koa\u6a21\u5757\nvar Koa = require('koa');\nvar Router = require('koa-router');\n\n\/\/\u5b9e\u4f8b\u5316\nvar app = new Koa();\nvar router = new Router();\n\n\/\/\u5339\u914d\u4efb\u4f55\u8def\u7531\uff0c\u5982\u679c\u4e0d\u5199next\uff0c\u8fd9\u4e2a\u8def\u7531\u88ab\u5339\u914d\u5230\u4e86\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u5411\u4e0b\u5339\u914d\napp.use(async (ctx,next)=>{\n    await next();\n    \/\/\u5982\u679c\u9875\u9762\u627e\u4e0d\u5230\n    if(ctx.status==404){\n        ctx.status = 404;\n        ctx.body=\"404 \u9875\u9762\"\n    }\n});\n\n\/\/\u914d\u7f6e\u8def\u7531\nrouter.get('\/', async (ctx) => {\n    ctx.body = \"\u9996\u9875\";\n});\nrouter.get('\/news', async (ctx) => {\n    ctx.body = \"\u65b0\u95fb\u5217\u9875\u9762\";\n});\n\n\/\/\u542f\u52a8\u8def\u7531\napp.use(router.routes());\napp.use(router.allowedMethods());\n\napp.listen(3000);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">if \u80fd\u4e0d\u80fd\u653e\u5230 next \u4e4b\u524d\uff1f\u653e\u5230\u524d\u9762\u65f6\uff0c\u4e0b\u6e38\u8fd8\u6ca1\u8dd1\uff0cstatus \u5f80\u5f80\u4ecd\u662f\u9ed8\u8ba4 404\uff0c\u4f1a\u76f4\u63a5\u62e6\u4f4f\uff0c\u5339\u914d\u4e0d\u5230\u540e\u9762\u7684\u8def\u7531\u3002\u8fd9\u6b63\u662f\u4e0b\u4e00\u8282\u6d0b\u8471\u6a21\u578b\u8981\u89e3\u91ca\u7684\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e94\u3001\u6d0b\u8471\u6a21\u578b\uff1a\u5148\u8fdb\u540e\u51fa<\/h2>\n<p style=\"margin: 1em 0px;\">Koa \u5e38\u88ab\u79f0\u4e3a\u7b2c\u4e8c\u4ee3 Node Web \u6846\u67b6\uff0c\u6700\u5927\u7279\u70b9\u5c31\u662f\u4e2d\u95f4\u4ef6\u6d41\u7a0b\u63a7\u5236\uff1a\u5148\u9010\u7ea7\u5904\u7406 request\uff0c\u5339\u914d\u5b8c\u8def\u7531\u540e\u518d\u9010\u7ea7\u5904\u7406 response\u3002\u767b\u5f55\u7cfb\u7edf\u4e5f\u662f\u8fd9\u4e2a\u601d\u8def\u2014\u2014\u5148\u628a\u8d26\u53f7\u5bc6\u7801\u9001\u8fdb\u540e\u53f0\uff0c\u6570\u636e\u5e93\u5339\u914d\u5b8c\u518d\u5904\u7406\uff0c\u5904\u7406\u5b8c\u518d\u8fd4\u56de\u524d\u7aef\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/\u5f15\u5165 koa\u6a21\u5757\nvar Koa = require('koa');\nvar Router = require('koa-router');\n\n\/\/\u5b9e\u4f8b\u5316\nvar app = new Koa();\nvar router = new Router();\n\napp.use(async (ctx, next) => {\n    console.log('1\u3001\u8fd9\u662f\u7b2c\u4e00\u4e2a\u4e2d\u95f4\u4ef601');\n    await next();\n    console.log('5\u3001\u5339\u914d\u8def\u7531\u5b8c\u6210\u4ee5\u540e\u53c8\u4f1a\u8fd4\u56de\u6765\u6267\u884c\u4e2d\u95f4\u4ef6');\n});\n\napp.use(async (ctx, next) => {\n    console.log('2\u3001\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u4e2d\u95f4\u4ef602');\n    await next();\n    console.log('4\u3001\u5339\u914d\u8def\u7531\u5b8c\u6210\u4ee5\u540e\u53c8\u4f1a\u8fd4\u56de\u6765\u6267\u884c\u4e2d\u95f4\u4ef6');\n});\n\nrouter.get('\/', async (ctx) => {\n    console.log('3\u3001\u5339\u914d\u5230\u4e86news\u8fd9\u4e2a\u8def\u7531');\n    ctx.body = \"\u9996\u9875\";\n});\n\n\/\/\u542f\u52a8\u8def\u7531\napp.use(router.routes());\napp.use(router.allowedMethods());\n\napp.listen(3000);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8bbf\u95ee <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">localhost:3000\/<\/code> \u65f6\uff0c\u63a7\u5236\u53f0\u987a\u5e8f\u662f 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5\u3002\u6240\u4ee5\u9519\u8bef\u5904\u7406\u8981\u628a status \u5224\u65ad\u653e\u5728 next \u4e4b\u540e\uff1a\u7b49\u8def\u7531\u8dd1\u5b8c\uff08\u6216\u6ca1\u5339\u914d\u4e0a\uff09\u518d\u51b3\u5b9a\u8981\u4e0d\u8981\u8f93\u51fa 404\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aKoa \u4e2d\u95f4\u4ef6\u9760 await next() \u5f80\u4e0b\u4f20\uff1b\u4e0d\u5199 next \u5c31\u505c\u3002\u6d0b\u8471\u6a21\u578b\u5148\u8fdb\u540e\u51fa\uff0c404 \u5224\u65ad\u5fc5\u987b\u653e\u5728 next \u4e4b\u540e\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u8bb2\u4e86 Koa \u8def\u7531\u3001GET \u4f20\u503c\u548c\u52a8\u6001\u8def\u7531\u3002\u8fd9\u4e00\u7bc7\u770b \u4e2d\u95f4\u4ef6\uff0c\u4ee5\u53ca Koa \u7279\u6709\u7684 \u6d0b\u8471\u6a21\u578b\uff1a\u8bf7\u6c42\u4e00\u5c42\u5c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[107,105],"tags":[208],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2379"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2379"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2379\/revisions"}],"predecessor-version":[{"id":3266,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2379\/revisions\/3266"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}