34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
const puppeteer = require('puppeteer');
|
|
|
|
const takeSnap = async (req, res) => {
|
|
console.log('req.body.url', req.body.url);
|
|
|
|
// Set CORS headers
|
|
res.setHeader('Access-Control-Allow-Origin', '*'); // Allow all origins
|
|
|
|
// Handle preflight request (OPTIONS)
|
|
if (req.method === 'OPTIONS') {
|
|
res.status(200).end();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const url = req.body.url;
|
|
const browser = await puppeteer.launch();
|
|
const page = await browser.newPage();
|
|
await page.goto(url, { waitUntil: 'networkidle2' });
|
|
|
|
await page.screenshot({ path: 'screenshot.png', fullPage: true });
|
|
|
|
await browser.close();
|
|
console.log('Screenshot saved as screenshot.png');
|
|
|
|
// Send a response back to the client
|
|
res.status(200).json({ message: 'Screenshot saved as screenshot.png' });
|
|
} catch (error) {
|
|
console.error('Error taking screenshot:', error);
|
|
res.status(500).json({ error: 'Failed to take screenshot' });
|
|
}
|
|
};
|
|
|
|
module.exports = takeSnap; |