Is there any reason you need to send the image as base64 data? multipart/form-data is kinda better in this situation and you probably won’t have any problems with limit uploading.
You can use multer as middleware
const multer = require('multer');
const upload = multer();
app.post("/", upload.single('image_input'), (req, res) => {
// just so you know that req.file contains information about the uploaded file
console.log(req.file);
});
That way you don’t have to convert the image to base64 anymore, besides being more effective too.
This is how the file in raw binary looks like, you just need to write it to an image file. We can use fs module’s writeFile function to write the data.
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const app = express();
const upload = multer();
app.post("/", upload.single('image_input'), (req, res) => {
// Make a little check to see if the file is present
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
// Than you write the file
fs.writeFile('uploaded_image.jpg', req.file.buffer, function(err) {
if (err) {
return res.status(500).send(err);
}
res.send('File uploaded successfully.');
});
});