koa koa-static 静态资源中间件
// 安装
npm install --save koa-static
app.js
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const bodyParser = require('koa-bodyparser')
const static = require('koa-static')
const app = new Koa()
const router = new Router()
app.use(views('html', {
extension: 'ejs'
}))
app.use(bodyParser())
app.use(static(__dirname + '/static'))
router.get('/', async (ctx) => {
await ctx.render('post')
})
router.post('/doAdd', async (ctx) => {
ctx.body = ctx.request.body
})
app.use(router.routes())
.use(router.allowedMethods())
app.listen(30001)
// post.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="css/basic.css">
</head>
<body>
<form action="/doAdd" method="post">
用户名:<input type="text" name="username">
<br>
密 码:<input type="password" name="password">
<br>
<button type="submit">提交</button>
</form>
</body>
</html>
效果图