【问题标题】:Error on Heroku while uploading images to mongoDB using Multer使用 Multer 将图像上传到 mongoDB 时 Heroku 出错
【发布时间】:2020-10-26 20:15:51
【问题描述】:

我开发了一个应用程序,用户可以使用 multer 上传图像并可以在主屏幕上检索/查看它们。我使用 MongoDB 作为我的数据库,在本地它运行良好。即使使用 MongoDB CL,它在本地运行良好。但是当我使用数据库连接链接将其部署到 Heroku 时,出现以下错误。

2020-07-06T16:21:38.008989+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=quiet-anchorage-51950.herokuapp.com request_id=146ff967-98bb-45e5-a234-4dbf9535abbe fwd="124.123.143.110" dyno= connect= service= status=503 bytes= protocol=https

这是我的 app.js:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const fs = require('fs');
var path = require('path');
var multer  = require('multer');

var storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads')
    },
    filename: (req, file, cb) => {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
var upload = multer({ storage: storage })

app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.set("view engine", "ejs");

mongoose.connect("mongodb+srv://<myname>:<removedpassword>@webdatabase.rbrhg.mongodb.net/<dbname>?retryWrites=true&w=majority", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

我用过的图片Schema:

const imgSchema = new mongoose.Schema({
    img: {
        data: Buffer,
        contentType: String
    }
});

我上传图片的发布请求:

app.post('/imgupload', upload.single('image'), function (req, res, next) {
    var image = new Image({
        img: {
            data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename)),
            contentType: 'image/png'
        }
    });
    image.save();
    res.render("app",{Name: req.body.username});
});

这是我的 package.json 文件:

{
  "name": "igwebsite",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "engines": {
    "node": "12.16.3"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.3",
    "express": "^4.17.1",
    "mongodb": "^3.5.9",
    "mongoose": "^5.9.21",
    "multer": "^1.4.2"
  }
}

问题是我希望我的本地完美网络应用能够在线运行,有人可以帮忙吗?

【问题讨论】:

    标签: node.js mongodb express heroku mongoose


    【解决方案1】:

    您需要将文件/图像存储在 aws s3 中 参考这个https://www.npmjs.com/package/multer-s3

    【讨论】:

      【解决方案2】:

      谢谢,但我解决了。我的问题是端口值,我使用的 OR 条件可能是错误的。也许它只是占用 3000 而不是 Heroku 提供的端口。

      我所做的只是改变了这个

      app.listen(3000 || process.env.PORT,() => {
         console.log("Server is up at port "+process.env.PORT);
      });
      

      对此:

      app.listen(process.env.PORT || 3000,() => {
         console.log("Server is up at port "+process.env.PORT);
      });
      

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 2020-09-03
        • 2021-04-07
        • 2021-06-10
        • 2020-06-14
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 2021-11-03
        相关资源
        最近更新 更多