Created
August 13, 2019 01:44
-
-
Save ryanlid/6c3c85224ae8c321c3728ac5d44e6890 to your computer and use it in GitHub Desktop.
koa 常用中间件
This file contains 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 views = require("koa-views"); | |
const path = require("path"); | |
const bodyParser = require("koa-bodyparser"); | |
const static = require("koa-static"); | |
const Router = require("koa-router"); | |
const app = new koa(); | |
const router = new Router(); | |
// 加载模版引擎 | |
app.use(views(__dirname + "/views"), { | |
map: { html: "ejs" } | |
}); | |
// 加载静态资源 | |
app.use(static(path.join(__dirname, "/static"))); | |
// 渲染模版 | |
router.get("/", async (ctx, next) => { | |
await ctx.render("index"); | |
}); | |
router.post("/", (ctx, next) => { | |
let postData = ctx.request.body; | |
ctx.body = postData; | |
}); | |
app | |
.use(bodyParser()) | |
.use(router.routes()) | |
.use(router.allowedMethods()); | |
app.listen(3000); |
Author
ryanlid
commented
Aug 13, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment