I have developed an app where the user can upload images using multer and can retrieve/view them on the home screen. I'm using MongoDB as my database, locally it works perfectly. Even when using MongoDB CL, It's running well locally. But when I deploy it to Heroku with the database connection link, I'm getting the following error.
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
Here is my 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,
});
The image Schema I have used:
const imgSchema = new mongoose.Schema({
img: {
data: Buffer,
contentType: String
}
});
The post request I made to upload the images:
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});
});
Here is my package.json file:
{
"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"
}
}
The Thing is I want my locally-perfect web app to work online, Can someone please help?