如何在nodejs中使用multer从Jimp上传图像

时间:2018-12-03 16:48:41

标签: node.js mongodb multer multer-gridfs-storage

我从客户端发送了一个请求,该请求带有主体中的图像链接,在节点服务器中,我使用jimp(Jimp NPM)调整发送的图像的大小,然后需要将图像保存到mongodb与multer合作,但我对此步骤有些困惑,有人知道吗?谢谢 ! 到目前为止,这是我的代码:

const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const mongoose = require('mongoose');

    exports.resizeImage = async function (req, res) {
        let storageFS = new GridFsStorage({
            db: app.get("mongodb"),
            file: (req, file) => {
                return new Promise((resolve, reject) => {
                    crypto.randomBytes(16, (err, buf) => {
                        if (err) {
                            return reject(err);
                        }
                        const filename =  file.originalname;
                        const fileInfo = {
                            filename: filename,
                            bucketName: 'images'
                        };
                        resolve(fileInfo);
                    });
                });
            }
        });
        var upload = multer({ storage: storageFS }).single('image');
        try {
            Jimp.read(req.body.imagePath)        
            .then(image => {
                image.resize(150, 150, function (err, img) {
                    // How to store img to mongodb with multer here ? 
                });
            })
            .catch(err => {
                throw err
            });


        } catch (error) {
            res.send(error);
        }
    }

0 个答案:

没有答案