在将MongoDB后端部署到Heroku的照片应用程序中使用临时内存

时间:2019-01-02 15:43:33

标签: mongodb express heroku multer

这是我的应用程序,用于将图像保存到 mongoDB 。我在应用程序中使用了multerexpress,并且已成功将其部署到heroku

这是我的代码:

const express = require('express');
const multer = require('multer');
const photo = require('../model/photo.js');
const fs = require('fs');
const path = require('path');

const router = express.Router();

process.env.PWD = process.cwd();

router.use(express.static(path.join(process.env.PWD,'public')));

const storage = multer.diskStorage({
    destination: function(req,res,cb) {
        cb(null,path.join(process.env.PWD,'public/upload'));
    }
});

const upload = multer({storage:storage});

router.get('/',(req,res) => {
    res.send("This is the api of photo gallery");
});

router.route('/all_data').post(upload.array('file'),(req,res) => {
    for(let i=0;i<req.files.length;i++)
    {
    let new_photo = new photo;
    new_photo.data = fs.readFileSync(req.files[i].path);
    new_photo.contentType = req.files[i].mimetype;
    new_photo.save();
    }
    res.send("New image added");
})
.get((req,res) => {
    photo.find({},(err,img) =>{
        if(err)
        throw err;
        res.contentType('json');
        res.send(img);
    });
});

module.exports = router;

我有一个问题。像这样使用临时内存(将临时映像保存在'/ public / upload'中保存)来部署应用程序到heroku上是个好主意吗?

0 个答案:

没有答案