Took me around 15 minutes in total. Can I be CTO at your silicon valley company now with 500k salary?
const express = require('express')
const app = express()
const puppeteer = require('puppeteer')
const uuidv1 = require('uuid/v1')
const fs = require('fs')
const validUrl = require('valid-url')
app.get('/', function (req, res) {
(async () => {
const url = req.query.url;
if (!url) {
return res.send({error: 'Need to specify url param e.g. ?url=https://google.com/'});
}
if (!validUrl.isUri(url)) { // Also protects against file://...
return res.send({error: 'url not valid'})
}
const id = uuidv1();
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080})
await page.goto(url)
const buffer = await page.screenshot({path: id + '.png'})
const fullPath = __dirname + '/' + id + '.png';
res.sendFile(fullPath, function () {
fs.unlink(fullPath)
})
await browser.close()
})();
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})