koa-better-body 简单使用

废话少说,直接代码

app.js

const Koa = require('koa')
const body = require('koa-better-body')
const path = require('path')
const static = require('koa-static')
const app = new Koa()

app.use(body({
    uploadDir: path.resolve(__dirname, "./upload")
}))

app.use(static(path.resolve('./upload')))

app.use(async ctx => {
    console.log(ctx.request.fields)
    ctx.body = "以一当十,是我精神"
})

app.listen(3000)

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="http://localhost:3000" method="post" enctype="multipart/form-data">
        username: <input type="text" name="username" /> <br>
        passoword: <input type="text" name="psw" /> <br>
        image <input type="file">
        <input type="submit" value="submit">
    </form>

</body>

</html>

发现图片没上传成功,

有时候真的,就这么一丁点问题,不信,你试试!

猜你喜欢

转载自blog.csdn.net/qq_15009739/article/details/108131874
今日推荐