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

jmockver

v1.0.0

Published

Run a mock server with JSON files, customize the response body and status code, all with JSON

Downloads

70

Readme

JMockver

Run a mock server with JSON files, customize the response body and status code, all with JSON

JMockver is a command-line tool that helps developers work independently by simulating server responses, usefull for frontend developers when the API development is not completed, only you need to now the responses structure to set this in a JSON files. It allows you to create a mock server that mimics the behavior of a real API, returning predefined data and responding to various HTTP requests. You can set any allow response body, sleep time to simulate response time, change response headers and more. You can intergrate to your fronted, mobile or backend app.

It is important to mention that no type of validation is carried out with the requests received, you can have multiple responses on an endpoint and only change the response id with the desired response.

NOTE: you will not have CORS problems since it allows requests from any origin.

CLI arguments

--dir             Folder to find JSON files. Default mocks/ in project root.
--port            Port number to run server. Default 3000
--logger          Enable to see HTTP request on console. Default false
--loggerFormat    Set a valid format to morgan package. Default tiny

To see Morgan package detail, click here

How to use?

  • Run command npm i jmockver -D to install as dev dependency
  • Create mocks/ folder in your project root, can change and set with --dir argument.
  • Into mocks/ folder create JSON files that you need (ex. users.json), can create multiple files with any name.
  • JSON files must be created with following template (should remove comments line in final JSON file)
{
  // URL endpoint to serve
  "/api/v1/users": {
    // Method to create on endpoint, can create GET, POST, PUT, PATCH and DELETE
    // NOTE: it is not require declare all methods, only those you need
    "GET": {
      "responseIdToReturn": "RESP001", // ID to response, this is finded into responses id property
      "sleep": 1000, // Sleep time, useful to simulate response time
      // Array with your multiple responses, can create that the ones you need
      "responses": [
        {
          "id": "RESP001", // Response id, for match with responseIdToReturn property
          "statusCode": 200, // Status code to return on response (200, 201, 400, 401, 500 etc etc). Default 200
          "body": [] // Body to return on response, can to be any valid JSON response (object, list, etc etc)
        },
        // You can create multiple responses into same responses property
        // Only need to change responseIdToReturn property with response ID that you want return
        { "id": "RESP002", "statusCode": 201, "body": {} },
        { "id": "RESP404", "statusCode": 404, "body": "Not found" }
        { "id": "RESP404", "statusCode": 500, "body": "Server error" }
      ]
    }
  }
}

Enter here to see another examples JSON files https://github.com/dadadev88/jmockver/tree/master/examples

  • After create JSON files, create a script in your package.json
{
  "scripts": {
    "mock": "jmockver" // You can add args to change default values
  }
}
  • Run script npm run mock or if not create a script npx jmockver, and you can see all readed JSON files with routes detail. Example:
[JMockver] ✅ Starting server on port 3000
[JMockver] 🧰 Run with arguments {"logger":true}
[JMockver] 🔎 Searching JSON mock files in "mocks" dir
[JMockver] 🗂️  Routes in products-v1.json file
[JMockver]   🚦 Creating mock /api/v1/products - GET
[JMockver]   🚦 Creating mock /api/v1/products/prd001 - GET
[JMockver]   🚦 Creating mock /api/v1/products/prd001 - DELETE
[JMockver] 🗂️  Routes in users-v1.json file
[JMockver]   🚦 Creating mock /api/v1/users - GET
[JMockver]   🚦 Creating mock /api/v1/users/1 - PUT
[JMockver]   🚦 Creating mock /api/v1/users/1 - POST
[JMockver]   🚦 Creating mock /api/v1/users/1 - DELETE
[JMockver] ✅ Server running on port 3000

If you set a responseIdToReturn with an id than not exists on responses list, this route will not be serve and you will see a similar message to this:

Example:

[JMockVer] ❌ Response with code RESP001 not found on GET /api/v1/users/3322

You can install globally with npm i jmockver -g to use jmockver from anywhere path or project, consider that it always requires finding the mocks folder.