-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (39 loc) · 1.34 KB
/
app.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require('express');
const app = express();
const sharp = require('sharp');
const axios = require('axios');
const exphbs = require('express-handlebars');
//setting up view engine
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
//setting up express static
app.use(express.static('public'))
//the landing renderer api
app.get('/', (req, res)=>{
res.render('interface', { layout: false});
});
//our main image processing api
app.post('/process', async (req, res) => {
console.log(req.query.imageUrl);
//grabbing the image
const imageResponse = await axios({ url: req.query.imageUrl, responseType: 'arraybuffer' })
const inputBuffer = Buffer.from(imageResponse.data, 'binary')
//processing the image
sharp(inputBuffer)
.resize({
height: 300,
width: 300,
fit: "contain",
background: "#fff"
})
.toFile(`./images/bazar-shodai-binarypoets-${Date.now()}.jpg`, (err, info) => {
if(err) throw err;
res.status(200).json({info: `Format: 'jpeg', Width: 300px, Height: 300px, Size: ${info.size}, Channels: ${info.channels}`});
console.log(info)
});
});
//firing our server
const PORT = process.env.PORT || 399;
app.listen(PORT, ()=>{
console.log(`Server fired on port ${PORT}`);
});