DS
Size: a a a
DS
r
NK
DS
DS
r
DS
DS
r
DS
const sharpImage = async function(req) {
const destination = 'images/';
if (req.file) {
const filename = `${crypto.pseudoRandomBytes(16).toString('hex')}${path.extname(req.file.originalname)}`;
await sharp(req.file.buffer)
.resize(900)
.toFile(
path.resolve(
destination,
filename
)
);
return {
filename,
path: path.join('images', filename),
originalname: req.file.originalname
};
}
};
NK
NK
const sharpImage = async function(req) {
const destination = 'images/';
if (req.file) {
const filename = `${crypto.pseudoRandomBytes(16).toString('hex')}${path.extname(req.file.originalname)}`;
await sharp(req.file.buffer)
.resize(900)
.toFile(
path.resolve(
destination,
filename
)
);
return {
filename,
path: path.join('images', filename),
originalname: req.file.originalname
};
}
};
const destination = 'images/';
NK
DS
DS
const destination = 'images/';
NK
NK
DS
const Koa = require('koa');структура папок:
const koaStatic = require('koa-static');
const path = require('path');
const mount = require('koa-mount');
const uploadsPath = path.join(__dirname, '../../uploads');
const app = new Koa();
const PORT = process.env.PORT || 1338;
app.use(cors());
app.use(mount('/uploads', koaStatic(uploadsPath)));
// router initialization
const upload = multer();
router.post('/save', upload.single('image'), async(ctx) => {
const destination = 'uploads/';
if (ctx.file) {
const filename = `${crypto.pseudoRandomBytes(16).toString('hex')}${path.extname(ctx.file.originalname)}`;
await sharp(ctx.file.buffer)
.resize(900)
.toFile(
path.resolve(
destination,
filename
)
);
return {
filename,
path: path.join('images', filename),
originalname: ctx.file.originalname
};
}
// Response logic
});
app.listen(PORT, () => {
console.log(`Server listening on port: ${PORT}`);
});
localhost:port/uploads/image.png
в примере использовался koa но разница с express не большаяr
ГФ
:
"^4.17.6"