-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
34 lines (30 loc) · 796 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require('dotenv').config();
const express = require('express');
const sharp = require('sharp');
const cors = require('cors');
const formidable = require('formidable');
const app = express();
app.use(cors());
app.use(express.json());
app.post('/file', async (req, res) => {
const form = formidable();
form.parse(req, (err, fields, files) => {
if (err) {
next(err);
return;
}
const imageInput = files.image.path;
const contentType = files.image.type;
sharp(imageInput)
.resize(512, 512)
.png()
.toBuffer()
.then((data) => {
const base64Data = data.toString('base64');
res.status(202).json({ b64Data: base64Data, contentType: contentType, extension: 'png' });
res.send(base64Data);
})
.catch((err) => console.log(err));
});
});
app.listen(4000);