POST及文件上传

封装了很多东西:

const Koa = require('koa');
const config = require('./config');
const opn = require('opn');
const network = require('./libs/network');

const Router = require('koa-router');
const {post, upload} = require('./libs/body');

let server = new Koa();

(async ()=>{
    server.context.db = await require('./libs/mysql')
    server.context.redis = require('./libs/redis')


    server.use(async (ctx, next)=>{
        try{
            await next();
        }catch(e){
            ctx.status = 500
            ctx.body = 'internal server error'
        }
    })
    
    let router = new Router()
    // post + no upload
    router.post('/api', post(), async ctx=>{
            console.log(ctx.request.fields);
        })

    // post + upload
    router.post(
        '/upload',
        ...upload({
            maxFileSize: 100 * 1024,
            sizeExceed: async ctx=>{
                ctx.body = 'aaa'
            },
            error: async ctx=>{
                ctx.body = 'error'
            }
        }),
        async ctx=>{
            console.log(ctx.request.fields)
            ctx.body = '上传成功'
            
        }
    )
    server.use(router.routes())

    // network
    server.use(async ctx=>{
        network.forEach(ip =>{
            if(config.port == 80) {
                console.log(`server running at ${ip}`);
            }else{
                console.log(`server running at ${ip}:${config.port}`);
            }
        })
        
    })
    server.listen(config.port)

    
    

    // opn(`http://localhost:${config.port}`)
})();

body:

const body = require('koa-better-body');
const convert = require('koa-convert');
const config = require('../config');

module.exports={
    post() {
        return convert(body({
            multipart: false,
            buffer: false,
        }));
    },
    upload(options) {
        options = options || {};
        options.uploadDir = options.uploadDir || config.upload_dir;
        options.maxFileSize = options.maxFileSize || 20 * 1024 * 1024;

        return [ 
            async (ctx, next)=>{
            try{
                await next()
            }catch(e){
                console.log(e.message)
                if(e.message.startsWith('maxFileSize exceeded')){
                    if(options.sizeExceed) {
                        await options.sizeExceed(ctx);
                    }else{
                        ctx.body = '文件过大'
                    }
                }else{
                    if(options.error) {
                        await options.error(ctx, e)
                    }else{
                        throw e;
                        // ctx.body = '其他错误'
                    }
                }
            }
        },
        convert(body({
            uploadDir: options.uploadDir,
            maxFileSize: options.maxFileSize,
        })),
    ];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值