Created
August 14, 2019 08:44
-
-
Save ryanlid/3bf1a4b9a4116a2bffd98f45c1c4e692 to your computer and use it in GitHub Desktop.
koa-router 基本使用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Koa = require("koa"); | |
const Route = require("koa-router"); | |
const app = new Koa(); | |
const router = Route(); | |
router | |
.get("/", async (ctx, next) => { | |
ctx.body = "Hello World"; | |
console.log("get"); | |
await next() | |
}) | |
.all("/", async (ctx, next) => { | |
console.log("all"); | |
await next() | |
}); | |
app.use(router.routes()); | |
app.listen(3000, () => { | |
console.log("Server is running at http://localhost:3000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
all() 方法可以用来设置请求头