10 — Everything
Kitchen-sink showcase: dataFile + cross-endpoint joins + URL params + middleware (logger / delay / errorInjection / custom post) + scenarios — composed in one server.
Run this chapter in 30 seconds
- Open in StackBlitz → — full Node sandbox in your browser, no install.
- Wait for
npm installto finish, then in the Terminal tab run:npx tsx examples/10-everything/server.ts - Paste any request from the Try it section below into the Terminal (use
curl— the StackBlitz preview port is forwarded).
Why this chapter
Each previous chapter isolates one feature. Real apps stack several. This one shows how they fit together without contention:
| Layer | Provided by |
|---|---|
| Source-of-truth data | dataFile (rooms.json, messages.json) |
| Public API | handler({ fn }) reading ctx.endpoint('/internal/*') |
| URL params | :roomId, * patterns |
| Cross-cutting concerns | logger + delay + errorInjection + custom post middleware |
| Variants | scenarios busy / empty |
What to read
Don't try to memorize this file. Skim the structure, find the layer you need, copy that snippet into your own server. The chapter exists as a reality check that the pieces compose cleanly — not as a starting template.
Run it
git clone https://github.com/yoyo-67/mockr
cd mockr/examples/10-everything
npx tsx server.tsrequests.http in the same folder lists the operations to try in order.
Try it
Open in StackBlitz → — paste each curl into the StackBlitz Terminal once npx tsx examples/10-everything/server.ts is running.
# list rooms
curl -s http://localhost:3010/api/rooms
curl -s 'http://localhost:3010/api/rooms?private=true'
# messages for a room
curl -s http://localhost:3010/api/rooms/1/messages
# send a message
curl -s -X POST http://localhost:3010/api/rooms/1/messages \
-H 'Content-Type: application/json' \
-d '{"author":"alice","text":"hello world"}'
# search across all rooms
curl -s 'http://localhost:3010/api/search?q=hello'
# aggregate stats
curl -s http://localhost:3010/api/stats
# scenarios
curl -s -X POST http://localhost:3010/__mockr/scenario \
-H 'Content-Type: application/json' \
-d '{"name":"busy"}'
curl -s http://localhost:3010/api/stats
curl -s -X POST http://localhost:3010/__mockr/scenario \
-H 'Content-Type: application/json' \
-d '{"name":"empty"}'
curl -s http://localhost:3010/api/statsAbout 5% of requests randomly return 500 (errorInjection middleware). Re-run if you hit one — that's the demo.
Where next
- README in the repo for the full API reference.
examples/for any feature you want to copy in isolation.- Issue tracker for bug reports / feature requests.