I have 2 NodeJS applications: one being an back end API and one being a front end application that parses the data from the API. Is there any way to upload an image from the front end application and have it end up in a back end application folder?
I have 2 NodeJS applications: one being an back end API and one being a front end application that parses the data from the API. Is there any way to upload an image from the front end application and have it end up in a back end application folder?
firstly you have install multer and then
npm install --save multer
app.js
const multer = require(‘multer’);
const imageUpload = multer({
storage: imageStorage,
limits: {
fileSize: 1000000 // 1000000 Bytes = 1 MB
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(png|jpg)$/)) {
// upload only png and jpg format
return cb(new Error('Please upload a Image'))
}
cb(undefined, true)
}
})
// For Single image upload
router.post('/uploadImage', imageUpload.single('image'), (req, res) => {
res.send(req.file)
}, (error, req, res, next) => {
res.status(400).send({ error: error.message })
})
i hope this will helpful for you