Mock REST API with Service Worker
Design fake API responses for front-end work: path parameters, templates, CRUD collections, an in-page test console and a ready mock-sw.js file.
S'executa al teu navegador. Els fitxers mai no surten del dispositiu. Mira com comprovar-ho
…
Com es fa
- 1Add endpoints
In Endpoints set the method, path pattern, status, delay and body; add headers if needed.
- 2Use templates
Insert {{params.id}}, {{query.x}}, {{body.y}}, {{uuid}} or {{now}} in bodies.
- 3Create collections
Add a CRUD collection with a base path and seed array.
- 4Try requests
In Try it choose a method, type a path and press Send; check the log.
- 5Export the worker
Set the path prefix and download mock-sw.js and the register snippet.
- 6Register in your app
Put the file in your public folder, register it in development and reload once.
Sobre aquesta eina
Front-end work often stalls while the back end is not ready, or when you need to see how the interface handles a slow response, an error page or an empty list. A mock API solves that: the app calls the same URLs, and something local answers with data you control. This tool lets you define endpoints with methods, path patterns, status codes, headers, delays and JSON bodies that can include values from the request. Collections give you a complete in-memory REST resource with list, read, create, update and delete. You can try everything immediately in the page, and when it behaves as you want, download a service worker file that serves the same mocks inside your own application during development.
Who this is for
Front-end and mobile web developers, testers who need repeatable error cases, and teams writing demos before the real API exists.
Endpoints
Each endpoint has a method (or ANY), a path pattern, a status, an optional delay in milliseconds, headers and a body. In paths, :id captures one segment and * matches the rest. The first enabled match wins, so put specific routes above general ones with the arrow button. Status 204 sends no body.
Templates
Bodies and header values can include {{params.id}}, {{query.page}}, {{body.name}} and {{headers.authorization}} from the request, plus {{now}} for an ISO timestamp, {{timestamp}} in milliseconds, {{uuid}} for a random id and {{int 1 100}} for a random whole number.
Collections
A collection turns a seed JSON array into a working resource. GET on the base path lists items and supports ?q= full-text search, field filters such as ?done=true and paging with _start and _limit. GET, PUT, PATCH and DELETE on /:id work on single items, and POST adds an item with the next numeric id. Changes stay in memory, so you can test a whole create-edit-delete flow.
Trying it here
While this page is open it answers fetch requests to the host mock.local. The Try it tab sends a request and shows status, headers, body and time, and the log lists every request with the route that matched. You can also call fetch on mock.local from the browser console of this page.
Why a downloaded service worker
Browsers only register a service worker from a script file served by the same site as the app it controls. A tool on shobfree.com therefore cannot install one for your localhost or staging site. mock-sw.js is that script: put it in your public folder, register it in development, and it answers matching requests under the path prefix you set, with the same endpoints, templates and collections.
Worked example
GET /api/users/7?fields=name matches /api/users/:id and returns status 200 with id 7, name "User 7", fields "name" and the current time. POST /api/todos with a title returns 201 and id 3, and GET /api/flaky waits 800 ms and returns 503, which is how you test a retry message.
Limits
This is a development tool, not a server: nothing is reachable from other devices, and data resets when the page or worker restarts. Service workers need https or localhost and do not intercept requests made before they activate on first load. Responses are not validated against an API schema.
Exemple resolt
GET /api/users/7 → 200 { "id": 7, "name": "User 7" }; GET /api/flaky → 503 after 800 ms.
En resum
- Categoria: Desenvolupament i dades
- S'executa al navegador, sense registre, sense pujar res.
- La informació que introdueixes es queda al dispositiu.
- Disponible en anglès.
- Contingut revisat per última vegada:
- Font oficial: W3C Service Workers specification (fetch event, respondWith) and the WHATWG Fetch standard; OpenAPI 3.1 for the export
Preguntes freqüents
Why can this page not register the worker for my app?
The Service Workers standard requires the worker script to come from the same origin as the pages it controls, so the file must live on your own site.
Does the mock work on my phone or a teammate's computer?
Only in the browser where the worker is registered. For shared mocks you need a real server.
Can I simulate slow or failing APIs?
Yes. Set a delay in milliseconds and any status code such as 429, 500 or 503.
How do I stop the service worker?
Remove the register call and unregister it in your browser's DevTools under Application, Service workers.
Can I export the mocks as documentation?
Yes. Use in your app includes an OpenAPI 3.1 skeleton with example responses.
Is any data sent to a server?
No. Requests to mock.local are answered inside this page; nothing leaves your browser.
