I am building a web application using the MERN stack (MongoDB, Express Server, ReactJS front end and NodeJS back end) and was wondering some good ways to store images from the back end.
In the past, I have used Firebase for authentication and storage directly from the front end. As I am handling my own user authentication model in MongoDB, is it possible to still use firebase storage and if so would it be from the front end or back end. If it was from the front end, how would I secure it without having firebase authentication?
Other options I have read into are storing images into MongoDB using GridFS or storing on the server using Multer.
Once I have a solution in mind, I will be able to read the docs and figure out how to get it done.
Any advice is appreciated.
In fact, more than 750% of developers consistently use the MERN stack in development, making it a highly popular and in-demand technological stack. The usability of robust technologies and the ability to create high-performance applications increase the demand for the stack.
It consists of MongoDB, ExpressJS, ReactJS, and NodeJS as its working components. Here are the details of what each of these components is used for in developing a web application when using MERN stack: MongoDB: A document-oriented, No-SQL database used to store the application data.
Create . env file and store in it the port and your atlas URI. Make a routes folder and create user. js inside it; also create the images folder inside the backend folder.
An option is to upload the image to Cloudinary in the client-side and save the returned URL to MongoDB with your own API. Cloudinary does more than hosting your images but also handles image manipulation and optimization and more.
Basically what you will have to do is:
Then your upload function can be something like this:
async function uploadImage(file) { // file from <input type="file">
const data = new FormData();
data.append("file", file);
data.append("upload_preset", NAME_OF_UPLOAD_PRESET);
const res = await fetch(
`https://api.cloudinary.com/v1_1/${YOUR_ID}/image/upload`,
{
method: "POST",
body: data,
}
);
const img = await res.json();
// Post `img.secure_url` to your server and save to MongoDB
}
I think using multer is the very convenient way.
You can upload the images into a folder using multer and store the reference URL in MongoDB. It is also important if you are willing to host your MERN application. You don't need any third party help like firebase or Cloudinary uploads and authentications (you have done this already).
So you can host your own app using your own functionalities. No external cost (just for the domain :D)
This may help you to get a brief idea.
const InstrumentImageStore = multer.diskStorage({
destination: function (req, file, callback) {
const userId = req.userId;
const dir = `instrumentImgs/${userId}`;
fs.exists(dir, (exist) => {
if (!exist) {
return fs.mkdir(dir, (error) => callback(error, dir));
}
return callback(null, dir);
});
},
filename: function (req, file, callback) {
callback(null, Date.now() + "-" + file.originalname);
},
});
router.post(
"/add/instrument",
[isauth, multer({ storage: InstrumentImageStore }).array("imageArr", 5)],
//isauth is another middleware that restricts requests using JWT
instrumentController.addInstrument
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With