NodeJS server to spit out HTML from loaded JS using Puppeteer
1.6K
Lightweight Docker image with NodeJS server to spit out HTML from loaded JS using Puppeteer and Chrome
Medium story: HTML from the Javascript world
| Image size | RAM usage |
|---|---|
| 558MB | 110MB+ |
The program is written in NodeJS with Typescript, in the src directory.
Runs a NodeJS server accepting HTTP requests with two URL parameters:
url which is the URL to prerender into HTMLwait which is the optional load event to wait for before stopping the prerendering. It can be:
load (wait for the load event)domcontentloaded (wait for the DOMContentLoaded event)networkidle0 (default, wait until there is no network connections for at least 500 ms)networkidle2 (wait until there are less than 3 network connections for at least 500 ms)For example:
http://localhost:8000/?url=https://github.com/qdm12/htmlspitter
amd64 for now and is required.Run the container
docker run -it --rm --init -p 8000:8000 qmcgaw/htmlspitter
You can also use docker-compose.yml.
| Name | Default | Possible values | Description |
|---|---|---|---|
MAX_PAGES | 10 | -1 or integer larger than 0 | Max number of pages per Chromium instance at any time, -1 for no max |
MAX_HITS | 300 | -1 or integer larger than 0 | Max number of pages opened per Chromium instance during its lifetime (before relaunch), -1 for no max |
MAX_AGE_UNUSED | 60 | -1 or integer larger than 0 | Max age in seconds of inactivity before the browser is closed, -1 for no max |
MAX_BROWSERS | 10 | -1 or integer larger than 0 | Max number of Chromium instances at any time, -1 for no max |
MAX_CACHE_SIZE | 10 | -1 or integer larger than 0 | Max number of MB stored in the cache, -1 for no max |
MAX_QUEUE_SIZE | 100 | -1 or integer larger than 0 | Max size of queue of pages per Chromium instance, -1 for no max |
LOG | normal | normal or json | Format to use to print logs |
TIMEOUT | 15000 | -1 or integer larger than 0 | Timeout in ms to load a page, -1 for no timeout |
If you obtain the error:
{"error":"Error: Failed to launch chrome!\nFailed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted\n\n\nTROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md\n"}
Then you might need to use seccomp with the chrome.json file of this repository:
wget https://raw.githubusercontent.com/qdm12/htmlspitter/master/chrome.json
docker run -it --rm --init --security-opt seccomp=$(pwd)/chrome.json -p 8000:8000 qmcgaw/htmlspitter
--init flag is added to prevent eventual zombie Chromium processes to exist when the container stops the main NodeJS program.node build/healthcheck.js against a running instance.# Install all dependencies
npm i
# Transcompile the Typescript code to Javascript and run build/main.js with
npm run start
Test it with, for example:
wget -qO- http://localhost:8000/?url=https://github.com/qdm12/htmlspitter
You can also:
Run tests
npm t
Run the sever with hot reload (performs npm run start on each .ts change)
npx nodemon
Build Docker
docker build -t qmcgaw/htmlspitter .
You can also specify the branch of Google Chrome from beta (default), stable and unstable
docker build -t qmcgaw/htmlspitter --build-arg GOOGLE_CHROME_BRANCH=unstable
There are two environment variables you might find useful:
PORT to set the HTTP server listening portCHROME_BIN which is the path to the Chrome binary or Puppeteer-bundledThis repository is under an MIT license
Content type
Image
Digest
Size
208.8 MB
Last updated
over 6 years ago
docker pull qmcgaw/htmlspitter