takeSnap
parent
1576fa92c1
commit
0735a91450
|
@ -2,9 +2,19 @@ const puppeteer = require('puppeteer');
|
|||
|
||||
const takeSnap = async (req, res) => {
|
||||
console.log('req.body.url', req.body.url);
|
||||
const puppeteer = require('puppeteer');
|
||||
|
||||
(async () => {
|
||||
// Set CORS headers
|
||||
res.setHeader('Access-Control-Allow-Origin', '*'); // Allow all origins
|
||||
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); // Allow POST and OPTIONS methods
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); // Allow Content-Type header
|
||||
|
||||
// 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();
|
||||
|
@ -14,8 +24,13 @@ const takeSnap = async (req, res) => {
|
|||
|
||||
await browser.close();
|
||||
console.log('Screenshot saved as screenshot.png');
|
||||
})();
|
||||
|
||||
}
|
||||
module.exports = takeSnap;
|
||||
// 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;
|
Loading…
Reference in New Issue