added base files, plus og experiment node script that uses commonjs not modules

This commit is contained in:
Robert McGovern 2024-03-06 11:48:48 +00:00
parent c295818039
commit 040346126c
3 changed files with 2175 additions and 0 deletions

57
express-puppeteer.js Normal file
View File

@ -0,0 +1,57 @@
const puppeteer = require("puppeteer");
const { scrollPageToBottom } = require("puppeteer-autoscroll-down");
const express = require("express");
const app = express();
const folderToServe = "/Users/tarasis/Programming/websites/rmcg.dev/www/";
let dynamicPort = 6100;
app.get("/screenshot", async (req, res) => {
//pretend generated html
const dynamicPage = express();
// console.log("🚀 ~ app.get ~ dynamicPage:", dynamicPage);
const currentDynamicPort = dynamicPort++;
console.log("🚀 ~ app.get ~ currentDynamicPort:", currentDynamicPort);
dynamicPage.use(
// express.static("/Users/tarasis/Programming/websites/rmcg.dev/www")
express.static(folderToServe)
);
const dynamicServer = dynamicPage.listen(currentDynamicPort, async () => {
console.log(
`Dynamic server is running at http://localhost:${currentDynamicPort}`
);
const browser = await puppeteer.launch();
// console.log("🚀 ~ dynamicServer ~ browser:", browser)
const page = await browser.newPage();
// await page.goto(req.query.url); // URL is given by the "user" (your client-side application)
await page.goto(`http://localhost:${currentDynamicPort}`); // URL is given by the "user" (your client-side application)
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:${currentDynamicPort}`
);
await dynamicServer.close();
});
});
app.listen(4000, () => {
console.log("opened server on 4000");
});

2099
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "motivational-picture-generator",
"version": "1.0.0",
"description": "A project to learn NodeJS, ExpressJS, and whatever else along the way.",
"main": "index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "Modules",
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.18.3",
"puppeteer": "^22.4.0",
"puppeteer-autoscroll-down": "^2.0.0"
}
}