const express = require('express');
const multer = require('multer');
const sharp = require('sharp');
const app = express();
// Multer configuration
const upload = multer({
limits: {
fileSize: 4 * 1024 * 1024,
}
});
app.post('/upload', upload.single('image'), async (req, res) => {
try {
const buffer = await sharp(req.file.buffer)
.resize({ width: 500, height: 500 })
.png()
.toBuffer();
// You can now write the buffer to a file, or save it to a database,
// or send it over the network, etc.
res.send('Image uploaded and resized');
} catch (err) {
res.status(400).send({ error: err.message });
}
});
app.listen(3000, () => {
console.log('Server is up on port 3000');
});
2条答案
按热度按时间bvjveswy1#
uujelgoq2#
下面是一个基本的Node.js服务器,它使用Multer和Sharp来调整上传图像的大小: