1

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

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);
        }
    }
4

0 回答 0