Set up a Portal

Preview and share a server running in a Pod with a Portal URL

This guide shows how to start a server inside a Pod and get a Portal URL to preview and share it.

Portals need an API key. Boot the Pod with one before you start.

This guide assumes you have a terminal.

1. Add a preview to your page

<div id="url">Waiting for Portal...</div>
<iframe id="preview"></iframe>

2. Register a Portal handler

Register a callback with onPortal to show the Portal URL and load it in the iframe:

const iframe = document.querySelector("#preview");
const urlDiv = document.querySelector("#url");
pod.onPortal(({ url, port }) => {
urlDiv.innerHTML = `Portal for port ${port}: <a href="${url}">${url}</a>`;
iframe.src = url;
});

3. Write the server code

Write a minimal server into the Pod (see Write files to the Pod):

await pod.createDirectory("/project");
const file = await pod.createFile("/project/server.js", "utf-8");
await file.write(`require("http")
.createServer((req, res) => res.end("Hello from the Pod"))
.listen(3000);`);
await file.close();

4. Start the server

When your server listens on a port, BrowserPod creates a Portal:

pod.run("node", ["server.js"], {
echo: true,
terminal,
cwd: "/project",
});

Notes

  • Open the Portal URL to preview the server, or share it with others.
  • If multiple servers listen on different ports, you will receive a callback for each Portal.