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

json-mock-server

v0.0.6

Published

A lightweight tool to spin up a simple mock server using JSON syntax - perfect for mocked development, offline development or unit testing.

Downloads

16

Readme

JSON Mock Server

GitHub license

A lightweight tool to spin up a simple mock server using JSON syntax - perfect for mocked development, offline development or unit testing.

Installation

Prerequisites: This module creates a Koa server. As a result it will require node v7.6.0 or higher.

Install with npm:

npm install --save-dev json-mock-server

Install with yarn:

yarn add json-mock-server --dev

Usage

You will need to create a .json/.js file containing the routes and responses for the server. A simple example will look like this:

{
  "/user": {
    "use": "GET",
    "responses": [
      {
        "status": 200,
        "query": {
            "id": "10"
        },
        "response": {
          "id": 10,
          "name": "John",
          "email": "[email protected]"
        }
      },
      {
        "status": 204,
        "response": "User Not Found"
      }
    ]
  }
}

Keys

    use - Defines the http method to use to accept the request (e.g. get, post)

    responses - An array of responses

Inside each response you can define:
    status - Http status code

    query - Query parameters that have to be matched in order for the request to match

    response - Data to be sent back in the response

The above example will return a users data from the '/user' endpoint if the query parameter of id is equal to 10. If the id is not 10, the server will fallback to a 204 - User Not Found response. If any other endpoint is hit, the server will respond with a 404 - Not Found.

Note: The fallback must be placed after the previous responses, otherwise the request with id === 10 will be caught by the 204 response criteria.

It is possible to provide the server with a ".js" file instead of a ".json" - you just need to make sure the default export is the server config object.

CLI

json-mock-server --config path-to-config-file --port 3434

--config || -c - Defines the path to the config file relative to the current directory. By default, the command will look for a mock-server.config.js file in the current directory.

--port || -p - The port that the server will run on. Defaults to 3001.

Node

const { start } =  require('./app');
const config = require('./server-config');

start(config, 3434);  // Port will default to 3001 if undefined

The module exposes four functions for the mock-server:

start(config, port)
Starts the JSON server with the provided config and port number. If no port number is provided, default to 3001.

add(routes)
Updates the JSON server with the provided routes. These should be in the same structure as the initial config. New routes with the same key as any existing ones will overwrite the old ones.

restore()
Any changes to the server routes since initialization (by using the add function) will be reset. The server will use the routes provided during start(...).

stop()
Terminates the server. All routes are cleared and the server will need to be re-initialized with a new config using the start function.

Examples

A number of example cases can be found in the github repository HERE
These include a basic JSON example, .js example and unit test example.

To run these examples, run nmp i or yarn inside the examples folder.