Questions about koa-router,module.exports exporting fn, to router.post ('/ url', fn))

< H2 > implement a picture upload and form data submission, but there is a problem < / H2 >

clipboard.png


clipboard.png

require,module.exports

require


module.exports


clipboard.png

clipboard.png

Mar.20,2021

const userInsertInto = async(ctx, next) => {
    var storage = multer.diskStorage({
            //  
            destination: function(req, file, cb) {
                cb(null, 'static/images/')
            },
            //  
            filename: function(req, file, cb) {
                var fileFormat = (file.originalname).split(".");
                cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
            }
        })
        //  
    var upload = multer({ storage: storage }).single('file');
    await upload(ctx)
        .then(result => {
            console.log(ctx.req.body);
            console.log(ctx.req.file);
            ctx.body = {
                data: ctx.req.file
            }
        })
        .catch(error => {
            console.log(error)
            ctx.body = false;
        })
}
Menu