代码中有些地方需要改进,比如上传的位置/var/nodeimageupload/Resize.js
,
const sharp = require('sharp');
const uuidv4 = require('uuid/v4');
const path = require('path');
class Resize {
constructor(folder) {
this.folder = folder;
}
async save(buffer) {
const filename = Resize.filename();
const filepath = this.filepath(filename);
await sharp(buffer)
.resize({
fit: sharp.fit.outside,
withoutEnlargement: true
})
.toFile(filepath);
return filename;
}
static filename() {
return `${uuidv4()}.png`;
}
filepath(filename) {
return path.resolve(`/var/www/html/${filename}`)
}
}
module.exports = Resize;
这里我们可以自定义位置
return path.resolve(`/var/www/html/${filename}`)
为了隐藏我们的upload路径,我们可以将默认的upload改成随机字符串,这样可以避免恶意用户使劲上传文件,导致服务器崩溃
把这两个地方的uplyaiusfrhgr978oad
替换成你自己的随机字符串即可
nodeimageupload\server.js (1 hit)
Line 14: app.use('/uplyaiusfrhgr978oad', router);
nodeimageupload\views\index.ejs (1 hit)
Line 9: <form method="post" action="uplyaiusfrhgr978oad/post" enctype="multipart/form-data">
效果如下: