首页 > 解决方案 > 如何在显示图像时调整使用 multer-gridfs-storage 存储的图像大小

问题描述

这些是以下路线的变量

const GridFsStorage = require("multer-gridfs-storage");
const Grid = require("gridfs-stream");
var conn = mongoose.createConnection(db, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
let gfs;
conn.once("open", () => {
  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection("uploads");
});

这是我目前正在阅读文件的方式

router.get("/image/:filename", (req, res) => {
  gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
    if (!file || file.length === 0) {
      return res.status(404).json({
        err: "No file exist",
      });
    }
    //check if it's a jpeg
    if (
      file.contentType === "image/jpeg" ||
      file.contentType === "img/png" ||
      file.contentType === "image/png"
    ) {
      //read output to browser
      const readstream = gfs.createReadStream(file.filename);
      readstream.pipe(res);
    } else {
      res.status(404).json({
        err: "Not an image",
      });
    }
  });
});

如果可能的话,我如何在使用任何外部模块在上述路线中显示图像时调整图像大小,或者其他解决方案也是可以接受的

标签: node.jsmongodbexpressgridfs-streammulter-gridfs-storage

解决方案


推荐阅读