Serve a narrow local application
Serve a narrow local application — Build a local AI chat web app. Bind only to loopback; Save server.mjs; Test service and missing routes. Practical assignment: Lab evidence.
Demo edition. The materials are undergoing editorial review.
Topics covered
- Bind only to loopback
- Save server.mjs
- Test service and missing routes
Bind only to loopback
Serve the lab on 127.0.0.1:3000. This makes the intended local boundary explicit; it is not authentication against every process on the same machine.
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 server.mjs
This server exposes only two exact static paths and a health route. Create an empty app.js until the client lesson; a missing file should produce an error.
The static allowlist intentionally rejects query-string variants and unknown paths. That keeps this lab small and prevents filesystem traversal from request paths. If you later introduce URL parsing or asset directories, add explicit route tests instead of removing the allowlist to make a missing asset work.
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.
import http from 'node:http';
import {readFile} from 'node:fs/promises';
const files={'/':['index.html','text/html; charset=utf-8'],'/app.js':['app.js','text/javascript; charset=utf-8']};
http.createServer(async(req,res)=>{try{
if(req.method==='GET'&&req.url==='/health'){res.writeHead(200,{'Content-Type':'text/plain'});return res.end('ok');}
// Insert the POST /api/chat branch here in the next lesson.
const item=req.method==='GET'?files[req.url]:null;if(!item){res.writeHead(404);return res.end('Not found');}
const data=await readFile(item[0]);res.writeHead(200,{'Content-Type':item[1],'X-Content-Type-Options':'nosniff'});res.end(data);
}catch{if(!res.headersSent)res.writeHead(500);res.end('Local server error');}}).listen(3000,'127.0.0.1');
// Terminal: node server.mjs
Test service and missing routes
Open health and the home page, then request a nonexistent file. A health response proves the app server, not the model. Test Ollama separately.
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.
curl -i http://127.0.0.1:3000/health
curl -i http://127.0.0.1:3000/missing
curl -sS http://127.0.0.1:11434/api/tags
Practical assignment
Lab evidence
Continue to learning