|
| 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 | + |
| 128 | + |
| 129 | +## Source code |
| 130 | + |
| 131 | +[View full source code on GitHub](https://github.com/leaningtech/browserpod-meta/tree/main/examples/expressjs) |
0 commit comments