npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mock-api-response

v1.0.30

Published

Mock responses for your APIs for testing and developing web applications and services

Downloads

35

Readme

Mock API Response

Mock Responses for your APIs

Usage

Import

var mockApiResponse = require('mock-api-response');

Initialize

Configuration options for initializing the Server

options = {
    port: <port-number>,
    headers: {}, 
    cors: {
        enabled: true,
        origin: '*',
        methods: 'POST, GET, DELETE, PUT, PATCH, OPTIONS',
        headers: 'Authorization, Content-Type'
    }
}
  • port Port on which the Server will listen for incoming calls. Default is 5200
  • headers Map of global HTTP Headers that will be a part of every Response sent by the Server
  • cors CORS configuration
    • enabled If enabled, Server will respond with CORS Headers to OPTIONS call on every Endpoint. Default true
    • origin Allowed origins (Comma-Separated). Default *
    • methods Allowed Methods (Comma-Separated). Default 'POST, GET, DELETE, PUT, OPTIONS'
    • headers Allowed headers (Comma-Separated). Default 'Authorization, Content-Type'

Initialize the Server like this

mockApiResponse.init(options);

Mock

To mock an API, just provide its path from the context-root, the request method, and its scenarios.

The mock() method can be called multiple times to mock multiple APIs.

scenarios

The scenarios are an array of the different responses for an API. They are syntactically similar to OpenAPI Documentation JSON with a few differences.

For each scenario you must specify the following properties

name

A name describing what you are mocking. This will be useful for logging purposes

parameters

A list of parameters that will be used to match an incoming request. If all parameters in an inconing call match the ones specified here, the Server will respond with the respone specified in the response property.

If no matching scenario is found, the Server will respond with 404 - Not Found.

Each parameter can be defined with the following properties

  • in Where the Server will look for this request parameter
    • query For query strings
    • body For request body (to be implemented)
    • path For path parameters (to be implemented)
  • name Name of the Parameter
  • value Expected value of the Parameter

response

  • statusCode The Response StatusCode to respond with
  • headers This is an map of header names and their values.
  • body The Response Body. This can be any JSON format

Example

// Import
const mockApiResponse = require('mock-api-response');

// Initialize
mockApiResponse.init({ 
    port: process.env.PORT || 5200,
    headers: {
        'x-app-id': '787234923'
    }
});

// Mock
mockApiResponse.mock('/api/product', 'GET', [
    {
        name: 'get-products-by-category_Electronics',

        parameters: [
            { in: 'query', name: 'category', value: 'Electronics' }
        ],

        response: {
            statusCode: 200,

            headers: {
                'x-category-id': 'GHNBSF2424VN'
            },

            body: {
                "category": "Electronics",
                "products": [
                    {
                        "name": "Cell-Phone-A",
                        "cost": 1334.53,
                        "inStock": true
                    },
                    {
                        "name": "Tablet",
                        "cost": 242.17,
                        "inStock": false
                    }
                ]
            }
        }
    },

    {
        name: 'get-products-by-category_Home',

        parameters: [
            { in: 'query', name: 'category', value: 'Home' }
        ],

        response: {
            statusCode: 200,

            headers: {
                'x-category-id': 'HGFG6834356'
            },

            body: {
                "category": "Home",
                "products": [
                    {
                        "name": "Food Processor",
                        "cost": 433,
                        "inStock": true
                    },
                    {
                        "name": "Lamp",
                        "cost": 29,
                        "inStock": true
                    }
                ]
            }
        }
    }]
);

Request

GET http://www.mock-server.com/api/product?category=Electronics

Response
HTTP/1.1 200 OK

x-app-id': 787234923
x-category-id: GHNBSF2424VN

{
    "category": "Electronics",
    "products": [
        {
            "name": "Cell-Phone-A",
            "cost": 1334.53,
            "inStock": true
        },
        {
            "name": "Tablet",
            "cost": 242.17,
            "inStock": false
        }
    ]
}

Request

GET http://www.mock-server.com/api/product?category=Home

Response
HTTP/1.1 200 OK

x-app-id': 787234923
x-category-id: HGFG6834356

{
    "category": "Home",
    "products": [
        {
            "name": "Food Processor",
            "cost": 433,
            "inStock": true
        },
        {
            "name": "Lamp",
            "cost": 29,
            "inStock": true
        }
    ]
}

All other Requests

HTTP/1.1 404 Not Found

x-app-id': 787234923