1

These are the variables for the route below the

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

Here is how I am currently reading the files

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",
      });
    }
  });
});

If it is possible how could I resize the images when displaying them in the above route using any external module or another solution would be acceptable too

0 Answers0