Created
December 4, 2018 06:59
-
-
Save RoyiNamir/c7c8e904eae3dacd848a6e3928729b32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Install express server2 | |
import express from 'express'; | |
import * as url from 'url'; | |
const sharp = require('sharp'); | |
var request = require('request'); | |
export {}; | |
var cors = require('cors'); | |
const app = express(); | |
app.set('port', (process.env.PORT || 5000)); | |
app.use(cors()); | |
app.get('*', function (req, res1) | |
{ | |
let imgPath = url.resolve('http://localhost', req.originalUrl); | |
request({ | |
url : imgPath, | |
encoding: null | |
}, function (err, res, bodyBuffer) | |
{ | |
const resizer = sharp(bodyBuffer) | |
.webp({quality: 90}) | |
.toBuffer() | |
.then((myBuffer) => | |
{ | |
res1.writeHead(200, {'Content-Type': 'image/webp'}); | |
res1.end(myBuffer, 'binary'); | |
}).catch(error=>console.log(error)); | |
res1.pipe(resizer); | |
}); | |
}); | |
app.listen(app.get('port'), function () | |
{ | |
console.log('app running on port', app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment