I'm trying to make an image resizing API through SharpJS and my code follows as per the following
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;
sharp(imageInput)
.resize(128, 128)
.toBuffer()
.then((data) => {
console.log(data);
res.end(data)
})
.catch((err) => console.log(err));
});
});
app.listen(4000);
What I actually want to know is how can I return the resized image to the client without actually saving it in server disk? The data is in the form of buffer as below, How do I convert it back to a file so that users could get their images and download it automatically.
// console.log(data)
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 80 00 00 00 80 08 06 00 00 00 c3 3e 61 cb 00 00 00 09 70 48 59 73 00 00 0b 13 00 00 0b 13 01 ... 12835 more bytes>
My React Code
import React, {useState} from 'react'
import axios from 'axios'
function App() {
const [state, setState] = useState({
file: null
})
const fileHandler = e => {
console.log(e.target.files[0]);
setState({...state, file: e.target.files[0]})
}
const fileUploader = () => {
const formData = new FormData()
formData.append('image', state.file, state.file.name);
axios.post('http://localhost:4000/file',formData).then((res) => {
console.log(res.data)
}).catch((err) => {
console.error(err)
})
}
return (
<div className="App">
<input type="file" onChange={fileHandler} />
<button onClick={fileUploader}>Send File</button>
</div>
);
}
export default App;