37 lines
906 B
JavaScript
37 lines
906 B
JavaScript
import Router from 'koa-router'
|
|
import { koaBody } from 'koa-body'
|
|
import path from 'path'
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
// 创建router实例
|
|
const router = new Router({prefix: '/test'})
|
|
|
|
router.get('/', async ctx=>{
|
|
const page = await fs.readFile(path.resolve(import.meta.dirname, 'test.html'), 'utf-8')
|
|
ctx.type = 'text/html'
|
|
ctx.status = 200
|
|
ctx.body = page
|
|
})
|
|
|
|
router.post('/upload', koaBody({multipart: true}), async ctx=>{
|
|
try{
|
|
console.log(ctx.request.body);
|
|
ctx.status = 200
|
|
ctx.body = JSON.stringify({msg: '文件上传成功'})
|
|
}catch(err){
|
|
ctx.status = 403
|
|
ctx.type = 'application/json'
|
|
ctx.body = JSON.stringify({msg: '上传文件发生错误'})
|
|
}
|
|
ctx.body = '接收数据成功'
|
|
})
|
|
|
|
router.post('/data', koaBody({multipart: true}), async ctx=>{
|
|
console.log(ctx.request.body);
|
|
ctx.status = 200
|
|
ctx.body = "发送成功"
|
|
})
|
|
|
|
export default router
|