{"id":2386,"date":"2020-02-01T18:42:42","date_gmt":"2020-02-01T10:42:42","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=2386"},"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%e5%85%ad-%e9%9d%99%e6%80%81%e8%b5%84%e6%ba%90","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2386","title":{"rendered":"koa2 \u4ece\u5165\u95e8\u5230\u8fdb\u9636(\u516d)-\u9759\u6001\u8d44\u6e90"},"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 POST \u548c <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;\">koa-bodyparser<\/code>\u3002\u9875\u9762\u91cc\u8fd8\u8981\u5f15\u5165\u5916\u90e8\u7684 js\u3001css\u3001\u56fe\u7247\u65f6\uff0c\u76f8\u5bf9\u8def\u5f84\u5728 Koa \u91cc\u7ecf\u5e38 404\u3002\u8fd9\u4e00\u7bc7\u7528 <strong style=\"color: #4870ac;\">koa-static<\/strong> \u628a\u9759\u6001\u76ee\u5f55\u6302\u51fa\u53bb\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6309\u4ee5\u524d\u5199\u9759\u6001\u9875\u7684\u4e60\u60ef\uff0c\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;\">index.ejs<\/code> \u91cc\u7528\u76f8\u5bf9\u8def\u5f84\u5f15 <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;\">style.css<\/code> \u548c\u4e00\u5f20\u56fe\u7247\uff0c\u6d4f\u89c8\u5668\u5b9e\u9645\u8bf7\u6c42\u7684\u662f <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;\">http:\/\/localhost:3000\/static\/css\/style.css<\/code> \u548c <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;\">http:\/\/localhost:3000\/static\/img\/1.jpg<\/code>\u3002app.js \u91cc\u6ca1\u6709\u5bf9\u5e94\u8def\u7531\uff0c\u6240\u4ee5\u63a7\u5236\u53f0\u4f1a\u62a5\u627e\u4e0d\u5230\u6587\u4ef6\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\u76f8\u5bf9\u8def\u5f84\u4e3a\u4ec0\u4e48\u5931\u8d25<\/h2>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u8fd9\u4efd app.js \u53ea\u914d\u4e86\u6a21\u677f\u548c\u9996\u9875\u8def\u7531\uff0c\u6ca1\u6709\u628a <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;\">\/static<\/code> \u6620\u5c04\u5230\u78c1\u76d8\u76ee\u5f55\u3002ejs \u91cc\u5199\u7684\u76f8\u5bf9\u8def\u5f84\uff0c\u5bf9\u6d4f\u89c8\u5668\u6765\u8bf4\u5c31\u662f\u518d\u6253\u4e00\u6761 HTTP \u8bf7\u6c42\uff0c\u5fc5\u987b\u6709\u4e2d\u95f4\u4ef6\u6216\u8def\u7531\u63a5\u4f4f\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\nconst Koa = require('koa');\nconst Router = require('koa-router');\nconst views = require('koa-views');\n\n\/\/\u5b9e\u4f8b\u5316\nconst app = new Koa();\nconst router = new Router();\n\n\/\/\u914d\u7f6e\u6a21\u677f\u5f15\u64ce\u4e2d\u95f4\u4ef6\napp.use(views('views', {\n    extension: 'ejs'\n}));\n\nrouter.get('\/', async (ctx) => {\n    await ctx.render('index', {});\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;\">\u4e8c\u3001\u7528 koa-static \u6302\u9759\u6001\u76ee\u5f55<\/h2>\n<p style=\"margin: 1em 0px;\">\u5148\u5b89\u88c5\u6a21\u5757\u3002\u518d\u7528 <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(static(...))<\/code> \u628a\u78c1\u76d8\u4e0a\u7684 static \u76ee\u5f55\u66b4\u9732\u51fa\u53bb\u3002\u4e2d\u95f4\u4ef6\u4f1a\u5148\u5c1d\u8bd5\u5339\u914d\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u627e\u4e0d\u5230\u518d\u4ea4\u7ed9\u540e\u9762\u7684\u8def\u7531\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;\">npm install koa-static --save<\/code><\/pre>\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\nconst Koa = require('koa');\nconst Router = require('koa-router');\nconst views = require('koa-views');\nconst static = require('koa-static');\n\n\/\/\u5b9e\u4f8b\u5316\nconst app = new Koa();\nconst router = new Router();\n\n\/\/\u914d\u7f6e\u9759\u6001\u8d44\u6e90\u4e2d\u95f4\u4ef6\napp.use(static(__dirname + \"static\"));\n\/\/\u914d\u7f6e\u6a21\u677f\u5f15\u64ce\u4e2d\u95f4\u4ef6\napp.use(views('views', {\n    extension: 'ejs'\n}));\n\nrouter.get('\/', async (ctx) => {\n    await ctx.render('index', {});\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;\"><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;\">__dirname<\/code> \u662f\u5f53\u524d\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u539f\u6587\u7b2c\u4e00\u5904\u5199\u6210\u4e86 <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;\">__dirname + \"static\"<\/code>\uff0c\u4e2d\u95f4\u5c11\u4e86\u8def\u5f84\u5206\u9694\u7b26\uff0c\u5e94\u5199\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;\">__dirname + \"\/static\"<\/code>\uff0c\u4e5f\u53ef\u4ee5\u5199\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;\">static(\".\/static\")<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6302\u4e0a\u4e2d\u95f4\u4ef6\u540e\uff0c\u6a21\u677f\u91cc\u7684\u5f15\u7528\u6539\u6210\u6307\u5411 static \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff08\u4f8b\u5982 <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;\">\/css\/style.css<\/code>\u3001<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;\">\/img\/1.jpg<\/code>\uff0c\u5177\u4f53\u4ee5\u4f60\u6302\u8f7d\u7684\u6839\u76ee\u5f55\u4e3a\u51c6\uff09\u3002\u6837\u5f0f\u548c\u56fe\u7247\u90fd\u80fd\u52a0\u8f7d\uff0c\u5c31\u8bf4\u660e\u9759\u6001\u8d44\u6e90\u5df2\u7ecf\u901a\u4e86\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;\">\u9636\u6bb5<\/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;\">\u6d4f\u89c8\u5668\u8bf7\u6c42<\/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;\">\u7ed3\u679c<\/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;\">\u672a\u6302 koa-static<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\/static\/css\/style.css\u3001\/static\/img\/1.jpg<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6ca1\u6709\u5bf9\u5e94\u8def\u7531\uff0c404<\/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;\">app.use(static(__dirname + &#8220;\/static&#8221;))<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6309 static \u6839\u76ee\u5f55\u627e css\/img<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6837\u5f0f\u548c\u56fe\u7247\u90fd\u80fd\u52a0\u8f7d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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\uff1a\u6a21\u677f\u91cc\u7684 css\/js\/\u56fe\u7247\u662f\u65b0\u7684 HTTP \u8bf7\u6c42\uff1b\u7528 koa-static \u628a\u78c1\u76d8\u76ee\u5f55\u6302\u4e0a\uff0c\u8def\u5f84\u8bb0\u5f97\u52a0 \/static \u5206\u9694\u7b26\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u8bb2\u4e86 POST \u548c koa-bodyparser\u3002\u9875\u9762\u91cc\u8fd8\u8981\u5f15\u5165\u5916\u90e8\u7684 js\u3001css\u3001\u56fe\u7247\u65f6\uff0c\u76f8\u5bf9\u8def\u5f84\u5728 [&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\/2386"}],"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=2386"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2386\/revisions"}],"predecessor-version":[{"id":3269,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2386\/revisions\/3269"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}