Docs / Pages and packages
A page with a server JSpark+
In JSpark+, a page can bring its own Node server, which the page reaches with fetch.
On this page
The server is Node code in the same Snippet as the page. The page calls it with a path such as fetch("/api/notes"), with no port or CORS to set up. In JSpark, a page with a server runs without it, and says that a page with its own server needs JSpark+. Its server Part still opens and edits.
Start from a + Server Starter
- Click + at the end of the tabs, or look at the Home screen.
- Pick Web Page + Server, or React + Server for a React app.
- Press ⌘↵ or Ctrl+Enter. The server starts, and the page loads beside the code.
A page with a server waits for you to press Run: Run as you type never starts a server, because a server reaches outside JSpark.
The server Part
The Part strip gets one more tab, Server, after the page’s own. It is real Node, in the Snippet’s Language, with your own access to your files and network.
A page and its server
- The server gets its port as
process.env.PORT. Listen on it, as the demo does:server.listen(port, "127.0.0.1"). - The page calls the server on its own origin: JSpark passes every path it doesn’t serve itself to the server. Start the path with
/:"/api/notes", not"api/notes". - JSpark keeps three paths for itself:
/harness.js,/pkg/…and/r<n>/…. - A request waits up to five seconds for the server to start listening.
- With no server running, a request gets a 502 answer: This page’s server isn’t running. Press ⌘↵ to start it; it stops when another Node.js snippet runs.
Press ⌘↵ or Ctrl+Enter to restart both. The page and the server start over together. After you change the server Part, the page waits, marked stale, until you press it.
Browser and Server output
The console has two tabs: Browser, for what the page prints, and Server, for what the server prints. A dot on the tab you are not looking at means it has new output; a red dot means an error.
When the server stops
- JSpark runs one Node Run at a time. Running another Node Snippet stops the server, and its console says Server stopped because another Node.js snippet ran. Press ⌘↵ to restart it. Coming back to the tab doesn’t restart it.
- Press Stop (⌘. or Ctrl+.) to end the server yourself.
- The server ends when JSpark quits.
- The Timeline isn’t offered: a server waits for requests and has no end to replay. Peeks in the server Part show no values.
See also Web pages and Outside Access.
The pictures show JSpark on a Mac. On Windows, the window looks a little different and uses Ctrl for ⌘, but everything works the same way.