Skip to content
🧩 REST API mocking using Node and JSON-defined responses
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

REST Mock 👻

License: MIT Known Vulnerabilities

REST API mocking using Node and JSON-defined responses (mock server)

Contents 📖

  1. Features
  2. Uses
  3. Getting Started
  4. Defining Endpoints
  5. Sequential Responses
  6. License

Features 👍

  • Describe endpoints with JSON “definitions” or request matchers, which will respond to any request matching its Path and Method with the Status, Headers, and Response Body set for that definition
  • Sequential Responses allow for different responses depending on how many times an endpoint has been called since server start

Uses 🛠

  • Decoupled development; start working on an app UI before backing services are available
  • Testing your app against edge cases from endpoints you do not control or failure states that are difficult to reproduce
  • Show a working prototype frontend to get user feedback, without having to build out a backend first

Getting Started 🚦

  1. Clone the repository
  2. Edit the definitions.json file to define endpoints/responses
  3. Open a terminal window in your cloned repository
  4. Run npm install (or first install npm, if you haven’t already)
  5. Run npm start
  6. Send your requests to http://localhost:3030

REST Mock Startup

Defining Endpoints 📝

  • The definitions.json file is where your endpoints are defined. This is a single JSON object with an array of Definition objects, which define the Path and Method for the endpoint, as well as properties for the response, or an array of Response objects for Sequential Responses.
interface Definition {
	path: string,
	method: string,
	status?: number,
	headers?: StringMap<string | number>,
	responseBody?: object,
	sequentialResponses?: Array<Response>,
  • If a Sequential Responses array is included, it will be used instead of the Status, Headers, and Response Body properties of the definition object.
  • There are examples in the definitions.json file to start, including setting headers and some sequential responses.
  • Definitions are read in from dist/definitions.json on server start, which is copied from the definitions.json file in the root project folder whenever npm start is run or the TypeScript project is compiled (tsc).

Sequential Responses 🔁

  • If the sequentialResponses array is defined in the endpoint definition, it will iterate through array elements, responding with the properties from each one, until it reaches the last element of the array. It will continue to respond with the properties from the final element, if the endpoint is called again.
  • The sequentialResponses property is an array of Response objects
interface Response {
	status: number,
	headers: StringMap<string | number>,
	responseBody: object,

License 📜

This project is licensed under the MIT License - see for details

⬆ back to top

You can’t perform that action at this time.