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

mocknobody

v2.0.4

Published

mocking local API server using 1 file

Downloads

6

Readme

mocknobody

npm version Dependencies Known Vulnerabilities

Creates api server in your local machine with 1 file route configuration. Live-reloads the mock server if API config changes. Does not do any process on request body (hence mocknobody).

Installation

npm i -g mocknobody

Creating API file

mocknobody -c

This will create api.js file, located by default in $HOME/.mocknobody/api.js. If you prefer to use your own api.js file, you can override this location by adding API_JS env variable e.g. API_JS=/var/mock/api.js mocknobody. api.js sample can be seen on api_sample.js.

Basic Usage

To start your mock server:

mocknobody

In separate terminal, you can try this example request

curl localhost:3000/ping

(if the command mocknobody is too long, you can add alias mock=mocknobody on your .bashrc or .zshrc file and use mock instead.)

api.js structure

api.js has this structure:

module.exports = {
  "routes": [
    {
      "method": "GET",
      "url": "/ping",
      "status": 200,
      "response": "pong",
    },
  ],
  "not_found": {
    "response": "not found",
  },
}
  • routes: array of route on your API, defined by 3 values:
    • method: case-insensitive http methods. Currently supports get, post, put, patch and delete.
    • url: relative url of your API. You can use same URL with different param queries in different routes.
    • status: integer of HTTP Response status code
    • response : can be string or a javascript object (will be returned JSON as default)
  • not_found: default response if the route is not found

Since api.js is imported just like normal javascript file by the server, you can add any logic on the file as you want. As long as the format exported is still the same as defined above, mocknobody will be able to parse them.

Configurations

All these options can be configured through environment variables

| Property | Description | Default value | |-|-|-| | PORT | Port to serve the mock server | 3000 | | API_JS | Overriding API_JS file with your own file | $HOME/.mocknobody/api.js |

Example usage with config:

PORT=3001 API_JS=/var/mock/api.js mocknobody

Running on Development mode

DEV=true ./src/wrapper.js

this will run mocknobody.js in src, as well as adding it and wrapper.js to file watch list.

Design decisions

  • why use api.js and not api.json?
    • In addition to enable you to be flexible with your logic by having javascript file instead of just json file, this will also enable you to comment out any routes/responses/anything while you develop your frontend. This is a tool for mocking APIs after all.

License

MIT