I'm trying to the images submitted through HTML form and then upload those in the imagekit through node app. I'm completely lost with the configuration. Any help would be greatly appreciated.
const app = express();
const multer = require('multer');
const path = require('path');
const upload = multer({
dest: "uploads/" // "uploads"
});
var ImageKit = require("imagekit");
var fs = require('fs');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}));
var imagekit = new ImageKit({
publicKey : "public_6ImvGNsOViPhZ*******",
privateKey : "private_IZ1pjwUR9F********",
urlEndpoint : "https://ik.imagekit.io/*****/"
});
app.get('/upload', (req, res) => {
res.render('index')
})
app.post('/upload', upload.single("image"), (req, res) => {
fs.readFile('uploads/' + req.file.filename, function(err, data) {
if (err) throw err; // Fail if the file can't be read.
imagekit.upload({
file : req.file, //required
fileName : req.file.filename + '.jpg', //required
tags: ["tag1", "tag2"]
}, function(error, result) {
if(error) console.log(error);
else console.log(result);
});
});
console.log(req.file.filename);
res.redirect('/upload');
})