Skip to content

Commit b12e7c6

Browse files
committed
browserpod: reorder docs sections
1 parent 6215596 commit b12e7c6

File tree

18 files changed

+138
-129
lines changed

18 files changed

+138
-129
lines changed
File renamed without changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: Set up a basic NPM-based project
3+
description: In this tutorial you will set up a simple NPM projext that serves an HTTP server using Express.js
4+
---
5+
6+
## Prerequisites
7+
8+
- [Download the template project](/browserpod/tutorials/expressjs.zip) and unzip it.
9+
- [Set up an HTTP server with COEP and COOP headers](/docs/guides/nginx)
10+
11+
## Project structure
12+
13+
The project consists of the following files and directories:
14+
15+
```plaintext
16+
expressjs/
17+
├── index.html
18+
└── project
19+
├── main.js
20+
└── package.json
21+
```
22+
23+
The `index.html` file is the main page that is using BrowserPod.
24+
25+
The `project` directory is the NPM-based project that we want to run inside our page.
26+
27+
It is a simple Express.js application that serves "hello world" over HTTP.
28+
29+
## NPM Project
30+
31+
```js title="package.json"
32+
{
33+
"name": "expressjs-tutorial",
34+
"version": "1.0.0",
35+
"description": "",
36+
"main": "main.js",
37+
"scripts": {
38+
},
39+
"author": "",
40+
"license": "MIT",
41+
"dependencies": {
42+
"express": "^5.1.0"
43+
}
44+
}
45+
```
46+
47+
```js title="main.js"
48+
const express = require("express");
49+
const app = express();
50+
const port = 3000;
51+
52+
app.get("/", (req, res) => {
53+
res.send("Hello World!");
54+
});
55+
56+
app.listen(port, () => {
57+
console.log(`Example app listening on port ${port}`);
58+
});
59+
```
60+
61+
## BrowserPod setup: `index.html`
62+
63+
```html title="index.html"
64+
<!doctype html>
65+
<html lang="en">
66+
<head>
67+
<meta charset="utf-8" />
68+
<title>BrowserPod ExpressJS Tutorial</title>
69+
</head>
70+
71+
<body>
72+
<div id="url">Waiting for portal...</div>
73+
<iframe id="portal"></iframe>
74+
<div class="console" id="console"></div>
75+
<script type="module">
76+
import { BrowserPod } from "https://rt.browserpod.io/1.0/browserpod.js";
77+
// Utility function to copy files from the HTTP server into the Pod's
78+
// filesystem
79+
async function copy_file(pod, path) {
80+
const f = await pod.createFile("/" + path, "binary");
81+
const resp = await fetch(path);
82+
const buf = await resp.arrayBuffer();
83+
await f.write(buf);
84+
await f.close();
85+
}
86+
87+
// Boot the Pod
88+
const pod = await BrowserPod.boot({ apiKey: "your-api-key" });
89+
90+
// Create a terminal and hook it to the console div.
91+
const terminalDiv = document.getElementById("console");
92+
const terminal = await pod.createDefaultTerminal(terminalDiv);
93+
94+
// Hook the portal to the preview iframe on creation
95+
const portalIframe = document.getElementById("portal");
96+
const urlDiv = document.getElementById("url");
97+
pod.onPortal(({ url, port }) => {
98+
urlDiv.innerHTML = `Portal available at <a href="${url}">${url}</a> for local server listening on port ${port}`;
99+
portalIframe.src = url;
100+
});
101+
102+
// Copy our project files
103+
await pod.createDirectory("/project");
104+
await copy_file(pod, "project/main.js");
105+
await copy_file(pod, "project/package.json");
106+
107+
// Install dependencies
108+
await pod.run("/npm/bin/npm.js", ["install"], {
109+
terminal,
110+
cwd: "/project",
111+
echo: true,
112+
});
113+
114+
// Run the server
115+
await pod.run("/project/main.js", [], {
116+
terminal,
117+
cwd: "/project",
118+
echo: true,
119+
});
120+
</script>
121+
</body>
122+
</html>
123+
```
124+
125+
## End result
126+
127+
![The index.html page rendered](/browserpod/tutorials/express.png)
128+
129+
## Source code
130+
131+
[View full source code on GitHub](https://github.com/leaningtech/browserpod-meta/tree/main/examples/expressjs)
File renamed without changes.

sites/browserpod/src/content/docs/11-reference/00-BrowserPod/01-run.md renamed to sites/browserpod/src/content/docs/13-reference/00-BrowserPod/01-run.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ class BrowserPod {
88
async run(
99
executable: string,
1010
args: Array<string>,
11-
opts: { terminal: Terminal, env?: Array<string>; cwd?: string, echo?: boolean }
11+
opts: {
12+
terminal: Terminal;
13+
env?: Array<string>;
14+
cwd?: string;
15+
echo?: boolean;
16+
}
1217
): Promise<Process>;
1318
}
1419
```
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ description: Create an Xterm.js-based terminal emulator for input/output
55

66
```ts
77
class BrowserPod {
8-
async createDefaultTerminal(
9-
consoleDiv: HTMLElement,
10-
): Promise<Terminal>;
8+
async createDefaultTerminal(consoleDiv: HTMLElement): Promise<Terminal>;
119
}
1210
```
1311

File renamed without changes.

0 commit comments

Comments
 (0)