I saw this great introduction and I implemented it similarly, but I implemented it well on the local server (development server), but Amazon ec2 server using cloudfront generates 502 error.
After checking the console, it seems that long data with buffer is not stored in the database. I don't know why it works on the local server but not on ec2. Is cloudfront the problem? How can I find a solution?
const express = require('express');
const router = express.Router();
const Post = require('../models/Post');
const multer = require('multer');
const fs = require('fs');
var path = require('path');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage });
// blog post creation
router.post('/', upload.single('image'), (req, res) => {
const { title, description, category, md } = req.body;
const requestPost = {
title,
description,
category,
content: md,
image: {
data: fs.readFileSync(
path.join(__dirname, '..', 'uploads/' + req.file.filename)
),
contentType: 'image/png',
},
};
// the below codes doesn't work. console.log() in this code didn't work
const post = new Post(requestPost);
post.save((err, post) => {
if (err) return res.status(400).json({ trial: false, err });
res.status(200).json({ trial: true, post });
});
});
I think the size of the ArrayBuffer in the image is too large for cloudfront to communicate with MongoDB atlas.
If my guess is correct, I wonder how cloudfront interferes with this when the database and ec2 communicate directly.
Please let me know if you don't understand this question. I'm frustrated because I can't figure out what the problem is. Have a nice day....!