-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (31 loc) · 1.23 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const puppeteer = require('puppeteer');
const app = express();
app.use(bodyParser.json());
app.post('/run-script', async (req, res) => {
try {
console.log('Incoming request body:', req.body);
const { script, url } = req.body;
if (!script || !url) {
console.error('Missing script or URL in request');
return res.status(400).send({ error: 'Script name and URL are required.' });
}
const scriptFunction = require(`./scripts/${script}`);
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
console.log('Launching script...');
const result = await scriptFunction(browser, url);
await browser.close();
console.log('Script executed successfully');
res.json({ result });
} catch (error) {
console.error('Error occurred:', error.message);
res.status(500).send({ error: 'An error occurred.', details: error.message });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});