Service Mocker
Service Mocker is an API mocking framework for frontend developers. With the power of service workers, we can easily set up mocking services without any real servers. It sets developers free from intricate workflows, complex documentations and endless proxies from server to server.
Q: Is Service Worker ready?
A: No, not yet.
Q: Is Service Mocker ready?
A: Yes! Welcome to the future!
Installation
Since you are likely to run Service Mocker only during development, you will need to add service-mocker
as a devDependency:
npm install service-mocker --save-dev
For legacy browsers, you may also need the polyfills:
npm install service-mocker-polyfills --save-dev
Features
- No server is required.
- Real HTTP requests and responses that can be inspected in modern browsers.
- express style routing system.
- IE10+ compatibility.
Hello new world
A typical mocker includes two parts: client
and server
. First, let's create a server script named server.js
:
// server.js; const router = ; router; // or you can use the shorthand methodrouter;
Then, we need to write a client script to connect to the server:
// app.js;; const client = ; clientready;
After that, create a .html
file and include ONLY the client script:
Now navigate your browser to your local dev server (e.g. http://localhost:3000
). Open the console and you will see the following messages:
> [mocker:modern] connection established
>
> Hello new world!
Welcome to the future 👏.
Working with webpack
While using webpack, it's recommended to use sw-loader to create a standalone server script:
;; const client = ; clientready;
Docs & Demos
Team
Dolphin Wood | Vincent Bel |