# Headless Browsers: Puppeteer HTTP Server

import { Tabs, TabsContent, TabsList, TabsTrigger } from "zudoku/ui/Tabs"

{/* vale off */}
{/* THIS FILE WAS AUTOGENERATED FROM THE PUBLIC EXAMPLE REPOSITORY. DO NOT EDIT THIS FILE DIRECTLY. */}


Headless browsers power web scraping, automated testing, search engine optimization (SEO) rendering, screenshotting, and document generation.
They are resource-intensive and usually run in short bursts, which makes them a good fit for instances that boot in milliseconds and scale to zero between jobs.

This guide shows you how to use [Puppeteer](https://pptr.dev/), a Node.js library which provides a high-level API to control browsers, including the option to run them headless (no UI).
The example wraps Puppeteer in an [Express](https://expressjs.com/) HTTP server that renders a URL or an HTML payload to PDF.

To run it, follow these steps:

1. Install the [unikraft CLI](/cli).
   You need a [BuildKit](https://github.com/moby/buildkit) builder. The easiest way to get one is via [Docker](https://docs.docker.com/engine/install/).
   Alternatively, you can also directly set up and use BuildKit, see the [quick start](https://github.com/moby/buildkit#quick-start).

2. Clone the [`examples` repository](https://github.com/unikraft-cloud/examples) and `cd` into the `examples/httpserver-node-express-puppeteer/` directory:

   ```bash
   git clone https://github.com/unikraft-cloud/examples
   cd examples/httpserver-node-express-puppeteer/
   ```

Make sure to log into Unikraft Cloud and pick a [metro](/platform/metros) close to you.
This guide uses `fra` (Frankfurt, 🇩🇪):

```bash
unikraft login
```


:::note
A Puppeteer instance on Unikraft Cloud requires 4GB to run.
Request an increase in the instance memory quota when you need more memory.
:::

When done, invoke the following command to deploy this app on Unikraft Cloud:

```bash
unikraft build . --output <my-org>/httpserver-node-express-puppeteer:latest
unikraft run --metro fra \
  -m 4G \
  -p 443:3000/tls+http \
  --scale-to-zero policy=idle,cooldown-time=1000,stateful=true \
  --image <my-org>/httpserver-node-express-puppeteer:latest
```

The output shows the instance address and other details:

```ansi
[1mmetro[22m:        fra
[1mname[22m:         httpserver-node-express-puppeteer-7afg3
[1muuid[22m:         7bb479d7-5b3e-444f-b07c-eae4da6f57cc
[1mstate[22m:        [38;2;144;161;185mstarting[0m
[1mimage[22m:        <my-org>/httpserver-node-express-puppeteer
[1mresources[22m:
[3m  memory[23m:     4096MiB
[3m  vcpus[23m:      1
[1mservice[22m:
[3m  uuid[23m:       996b9cc1-5a51-e707-d443-5c98ea86ded8
[3m  name[23m:       little-snow-7qwu6vv5
[3m  domains[23m:
[3m  - fqdn[23m:     little-snow-7qwu6vv5.fra.unikraft.app
[1mnetworks[22m:
[3m- uuid[23m:       034fa25e-9154-7842-ccdd-289256cc7a17
[3m  private-ip[23m: 10.0.3.1
[3m  mac[23m:        12:b0:8f:3c:f5:16
[1mtimestamps[22m:
[3m  created[23m:    just now
```

In this case, the instance name is `httpserver-node-express-puppeteer-7afg3`.
They're different for each run.

Use a browser to access the landing page of the Puppeteer (that uses [ExpressJS](https://expressjs.com/)).
The app and the landing page are part of [this repository](https://github.com/christopher-talke/node-express-puppeteer-pdf-example).

In the example run above the landing page is at `https://nameless-fog-0tvh1uov.fra.unikraft.app`.
You can use the landing page to generate the PDF version of a remote page.

You can list information about the instance by running:

```bash
unikraft instances list
```

```ansi
[1mMETRO[22m  [1mNAME[22m                                     [1mSTATE[22m    [1mIMAGE[22m                                       [1mARGS[22m  [1mMEMORY[22m   [1mVCPUS[22m  [1mFQDN[22m                                    [1mCREATED[22m
fra    httpserver-node-express-puppeteer-7afg3  [38;2;0;188;125mrunning[0m  <my-org>/httpserver-node-express-puppeteer        4096MiB  1      nameless-fog-0tvh1uov.fra.unikraft.app  2 minutes ago
```

When done, you can remove the instance:

```bash
unikraft instances delete httpserver-node-express-puppeteer-7afg3
```

## Customize your deployment

The current deployment uses an ExpressJS service that uses the [PDF generating functionality of Puppeteer](https://devdocs.io/puppeteer/).
Customizing the deployment means updating the service, such as adding new functionalities provided by Puppeteer.
You can update the service to provide a REST-like interface.

## Learn more

Use the `--help` option for detailed information on using Unikraft Cloud:

```bash
unikraft --help
```

Or visit the [CLI Reference](/cli/unikraft).
{/* vale on */}
