Free lesson

Draw the boundary and build the interface

Draw the boundary and build the interface — Build a local AI chat web app. Fix the model endpoint on the server; Save index.html; Plan observable states. Practical assignment: Lab evidence.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Fix the model endpoint on the server
  • Save index.html
  • Plan observable states

Fix the model endpoint on the server

The browser calls /api/chat on the same origin. Only the Node backend contacts 127.0.0.1:11434. Do not proxy arbitrary URLs supplied by visitors.

The browser displays messages, the application server validates requests, and Ollama generates text. Keep these responsibilities separate so a browser cannot select arbitrary model endpoints or access server files. Use synthetic workshop questions during development. A model answer is generated text, not verified advice. The local boundary reduces external transfer but does not protect against every local user, browser extension or configured integration.

Save index.html

Create the interface with a labeled prompt and explicit stop/clear controls. Chat output uses plain text. The same English fixture is shared across technical examples.

Keep the upstream address out of request JSON entirely. This avoids turning the application into an arbitrary URL fetcher. The server can later expose a small allowed model list, but it must map trusted names to fixed configurations rather than accept a full URL from the browser.

Create the named files in one lab directory and run Node from that directory. This example fixes the model name and upstream address on the server. Before streaming, validate input and upstream status; after streaming starts, errors must be represented inside the stream or by a visible interrupted state. Keep byte limits, message limits and model context limits distinct, because each controls a different resource.

<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Local chat lab</title><style>body{font:1.1rem/1.6 system-ui;max-width:55rem;margin:2rem auto;padding:1rem}textarea{width:100%;font:inherit}pre{white-space:pre-wrap;overflow-wrap:anywhere}button{font:inherit;margin:.3rem}:focus-visible{outline:3px solid #9b4300}</style><main><h1>Local chat lab</h1><p>Generated answers can be wrong. Synthetic data only.</p><form id="chat"><label for="prompt">Message</label><textarea id="prompt" maxlength="2000" required></textarea><button id="send">Send</button></form><button id="stop" type="button">Stop</button><button id="clear" type="button">Clear session</button><p id="status" role="status"></p><div id="log"></div></main><script src="app.js" defer></script></html>

Plan observable states

Use idle, receiving, complete, interrupted and failed states. A disabled Send button prevents duplicate submits during one stream; server limits remain necessary.

Exercise a successful reply, stopped service, interrupted reply and oversized input. Preserve partial text as partial, not as a completed answer. The interface keeps history only in memory and provides a clear action; that does not erase operating-system logs or external backups. Before any public deployment add authentication, authorization, TLS, quotas, persistence policy and operational monitoring. A local demo does not prove those controls exist.

Practical assignment

Lab evidence

Continue to learning

Course outline

  1. Draw the boundary and build the interface
  2. Serve a narrow local application
  3. Proxy a bounded model stream
  4. Decode streaming records in the browser
  5. Bound sessions, input and resource use
  6. Verify the app and plan deployment