When developing a frontend application, usually we create mocks for backend API, so that after the API contract is settled down, front and backend engineers can work independently. There are several ways to accomplish this task, such as start a dedicated server and let the build tool serve as a proxy, or we can add middleware directly into the build tool’s dev server, if applicable. Some tools can monkey patch the network calls to replace the response with mock data, and various unit testing tools provide their own way of mocking. In this article, I will focus on how to add middleware into Parcel‘s dev server to respond with mock data for API calls.
API Proxy in Parcel’s development server
Parcel provides a dev server and supports API proxy out of the box. Under the hood, it uses connect and http-proxy-middleware to redirect API calls to a different server. It also provides the ability to customize the proxy behavior. For instance, by creating a file named .proxyrc.js
in project’s root, we can manually redirect all API calls to a mock server listening on localhost:8080
.
1 | const { createProxyMiddleware } = require('http-proxy-middleware') |
In order to serve API calls directly in Parcel’s dev server, we just need to write our own middleware and wire it into the connect
instance. Let’s name it mock-middleware
, and it has the following functions:
- Read source files from the
/mock
folder, and serve API calls with mock data. - When the files are updated, refresh the APIs as well.
Define mock files
1 | // /mock/user.js |
Mock API are simple functions that accept standard Node.js request/response objects as parameters and receive and send data via them. The function is associated with a route string that will be used to match the incoming requests. To ease the processing of request and response data, we use body-parser to parse incoming JSON string into req.body
object, and use send-data utility to send out JSON response, that helps setup the Content-Type
header for us. Since body-parser
is a middleware, we need to wire it into the connect
app, before the mock-middleware
we are about to implement.
1 | // /.proxyrc.js |
Create router
To match the requests into different route functions, we use route.js.
1 | // Create router and add rules. |
route.js
supports parameters in URL path, but for query string we need to parse them on our own.
1 | module.exports = { |
Combined with glob, we scan files under /mock
folder and add all of them to the router.
1 | glob.sync('./mock/**/*.js').forEach((file) => { |
Watch and reload
The next feature we need to implement is watch file changes under /mock
folder and reload them. The popular chokidar package does the watch for us, and to tell Node.js reload these files, we simply clear the require
cache.
1 | const watcher = chokidar.watch('./mock', { ignoreInitial: true }) |
Now that we have all the pieces we need to create the mock-middleware
, we wrap them into a class and provide a createMockMiddleware
function for it. The structure is borrowed from HttpProxyMiddleware. Full code can be found on GitHub.
1 | // /mock-middleware/index.js |
Standalone mock server
If you prefer setting up a dedicated server for mock API, either with Express.js or JSON Server, it is easy to integrate with Parcel. Let’s create a simple Express.js application first.
1 | // /mock-server/index.js |
To start the server while watching the file changes, use nodemon.
1 | yarn add -D nodemon |
Now configure Parcel to redirect API calls to the mock server.
1 | // /.proxyrc.json |
Use concurrently to start up Parcel and mock server at the same time. In fact, it is more convenient to create a npm script for that. Add the following to package.json
:
1 | { |