base code set to use import rather than require. Now to evolve the code
This commit is contained in:
parent
040346126c
commit
ebae4cc90b
|
@ -1,3 +1,6 @@
|
||||||
|
### Personal rmcg
|
||||||
|
screenshot.png
|
||||||
|
|
||||||
# ---> Node
|
# ---> Node
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
import puppeteer from "puppeteer";
|
||||||
|
import { scrollPageToBottom } from "puppeteer-autoscroll-down";
|
||||||
|
import express from "express";
|
||||||
|
import "dotenv/config";
|
||||||
|
|
||||||
|
const folderToServe = "/Users/tarasis/Programming/websites/rmcg.dev/www/";
|
||||||
|
|
||||||
|
const port = process.env.PORT || 4000;
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(express.static(folderToServe));
|
||||||
|
app.get("/screenshot", async (req, res) => {
|
||||||
|
const dynamicPage = express();
|
||||||
|
dynamicPage.use(express.static(folderToServe));
|
||||||
|
|
||||||
|
const dynamicServer = dynamicPage.listen(0, async () => {
|
||||||
|
const dynamicPort = dynamicServer.address().port;
|
||||||
|
console.log(
|
||||||
|
`Dynamic server is running at http://localhost:${dynamicPort}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const browser = await puppeteer.launch();
|
||||||
|
const page = await browser.newPage();
|
||||||
|
await page.goto(`http://localhost:${dynamicPort}`);
|
||||||
|
|
||||||
|
await page.waitForResponse((response) => response.status() === 200);
|
||||||
|
|
||||||
|
await scrollPageToBottom(page);
|
||||||
|
|
||||||
|
const screenshotBuffer = await page.screenshot({ fullPage: true });
|
||||||
|
|
||||||
|
// Respond with the image
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": "image/png",
|
||||||
|
"Content-Length": screenshotBuffer.length,
|
||||||
|
});
|
||||||
|
res.end(screenshotBuffer);
|
||||||
|
// other option is just to save the file to disk
|
||||||
|
// await page.screenshot({ path: 'example.png' });
|
||||||
|
// Close the browser
|
||||||
|
|
||||||
|
await browser.close();
|
||||||
|
console.log(`Closing dynamic server http://localhost:${dynamicPort}`);
|
||||||
|
|
||||||
|
await dynamicServer.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running at http://localhost:${port}`);
|
||||||
|
});
|
|
@ -4,16 +4,17 @@
|
||||||
"description": "A project to learn NodeJS, ExpressJS, and whatever else along the way.",
|
"description": "A project to learn NodeJS, ExpressJS, and whatever else along the way.",
|
||||||
"main": "index.mjs",
|
"main": "index.mjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"dev": "nodemon index.mjs",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"type": "Modules",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.18.3",
|
"express": "^4.18.3",
|
||||||
"puppeteer": "^22.4.0",
|
"puppeteer": "^22.4.0",
|
||||||
"puppeteer-autoscroll-down": "^2.0.0"
|
"puppeteer-autoscroll-down": "^2.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue