我正在尝试通过 HTML 表单提交的图像,然后通过节点应用程序将它们上传到 imagekit 中。我完全迷失了配置。任何帮助将不胜感激。
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');
})