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 🙏

© 2024 – Pkg Stats / Ryan Hefner

jmock

v1.0.4

Published

A simple command-line http server for mocking data, proxying requests and serving static files

Downloads

25

Readme

npm npm downloads license

中文文档

jmock

jmock is a simple command-line http server for mocking data, proxying requests and serving static files.

cute jmock

Installation

Running on-demand:

Using npx you can run the script without installing it first:

npx jmock [path] [options]

Globally via npm (RECOMMENDED)

npm install --global jmock

This will install jmock globally so that it may be run from the command line anywhere.

As a dependency in your npm package:

npm install jmock

Usage

jmock [path] [options]

[path] defaults to ./public if the folder exists, and ./ otherwise.

Now you can visit http://localhost:8080 to view your server

Use a specified port:

jmock -p 8082

Enable CORS via the Access-Control-Allow-Origin header:

jmock --cors

Open path after starting the server:

jmock -o /path

Generate a default configuration file with example code:

jmock --config

The above code will generate a file named jmock.config.js with example configuration code (easy to understand). It's used for mocking data and proxying requests. For details, please refer to the introduction below.

Mock data as HTTP response:

Create a file named jmock.config.js (if not existed) at the path where you run the jmock command. Then add a field mockTable as below and then rerun the jmock command:

Tip: you can make use of the function arguments: req, query, body, method, and Mock (Mock.js is a convenient tools used for generating mocking data).

module.exports = {
    // you can write your own logic code and return json as response, mock.js is out of the box as the Mock argument
    mockTable: {
        // eslint-disable-next-line no-console
        '/api/hello': ({ req, query, body, method, Mock }) => {
            return {
                code: 200,
                data: {
                    method,
                    query,
                    body,
                    data: Mock.mock({
                        // list is an array contains 1~10 elements
                        'list|1-10': [{
                            // id is a number whose initial value is 1, and is increased by 1 each time
                            'id|+1': 1
                        }]
                    }),
                },
                message: 'success',
            }
        },
        // you can also use async/await here
        '/api/world': async ({ req, query, body, method, Mock }) => {
            // delay reply after 300ms
            await new Promise((resolve) => {
                setTimeout(resolve, 300)
            })
            if (method === 'GET') {
                return {
                    code: 200,
                    data: {
                        method,
                        query,
                        body,
                        data: Mock.Random.paragraph(3, 7),
                    },
                    message: 'success',
                }
            }
            return {
                code: 200,
                data: {
                    method,
                    query,
                    body,
                    data: Date.now(),
                },
                message: 'it\'s not a GET request.',
            }
        }
    },
}

With the mockTable configuration above, http request to path /api/world as below:

fetch("/api/world?c=1&d=hello",
        {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            method: "POST",
            body: JSON.stringify({a: 11, b: 22})
        })
        .then((res) => {
            return res.json()
        })
        .then((res) => {
            console.log(res.data)
        })
        .catch((res) => {
            console.log(res)
        })

will get response data like:

{
  "code": 200,
  "data": {
    "method": "POST",
    "query": {
      "c": "1",
      "d": "hello"
    },
    "body": {
      "a": 11,
      "b": 22
    },
    "data": 1706670742590
  },
  "message": "it's not a GET request."
}

Proxy HTTP requests:

Create a file named jmock.config.js (if not existed) at the path where you run the jmock command. Then add a field proxyTable as below and then rerun the jmock command:

module.exports = {
    // proxy your requests
    proxyTable: {
        // the below configuration will proxy /baidu-search?wd=keyword to https://www.baidu.com/s?wd=keyword
        '/baidu-search': {
            target: 'https://www.baidu.com',
            changeOrigin: true,
            pathRewrite (path) {
                return path.replace('/baidu-search', '/s')
            },
        },
        // the below configuration will proxy /search?q=keyword to https://cn.bing.com/search?q=keyword
        '/search': {
            target: 'https://cn.bing.com',
            changeOrigin: true,
            cookieDomainRewrite: '',
        },
    },
}

Thanks

jmock is built on top of http-server.