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

development-mock-server

v1.0.4

Published

Server for development. Set mocks, link to direct files and send data via sockets

Downloads

12

Readme

Preamble

The main appointment of this server is mocking API requests. I developed it for my own purposes (because others mock servers don't satisfy me) and then I decided to make it public.

With this server you can

  • set mocks for requests that satisfy RegExp pattern
  • serve local files
  • broadcast messages to connected clients via socket
  • make POST or GET request with custom Origin & Referer headers
  • server on http or https
  • set options as CLI parameters or in a config file

Usage

Mocking

For mocking you must define some config.json file and pass it as CLI parameter --config=<path_to_config> (of course you can name it as you wish),

CLI command development-mock-server --config=./mocks/config.json

// config.json file or mock-server-config.json
{
    "rules": {
        "\/stream?.+id=8000": "./streamMock.xml",
        "[asd]{2}\/get.{1,3}": "/Users/user/my-project/mocks/mock.json"
    }
}

Pay attention that the property's name is RegExp, so no matter what you'll enter there, all will be converted into a regular expression. In property's value, you enter a path to mock, it can be relative or absolute. If development-mock-server runs as a module from node_modules, the relative path will relate to root directory of your project. If you clone it and run something like node development-mock-server/src/index.js, relative path will relate to development-mock-server root, and yes, this is evidently.

But you can redefine root directory for relative paths! Just add to your config file mocks property.

// config.json
{
    "mocks": "./mocks-dir", // relative path, the same rules as i wrote above
    // so if you use this module as dependecy just remember that the ROOT
    // will be project ROOT

    // or absolute
    "mocks": /Users/user/my-project/mocks-dir"

    "rules": {
        "\/stream?.+id=8000": "./streamMock.json",
        "[asd]{2}\/get.{1,3}": "/Users/user/my-project/mocks/mock.xml"
    }
}

API url

This is the main url

{
  "api": "https://api.somedomain.com/"
  "rules": {
    "[asd]{2}\/get.{1,3}": "/Users/user/my-project/mocks/mock.xml"
  }
}

So if you make request http://localhost:1234/get/info?p=1 the server will make a request to https://api.somedomain.com/get/info?p=1 and get your response. Also, it rewrites Origin and Referer headers (about this below). For example, if request will be http://localhost:1234/as/get332 it will satisfy the rule [asd]{2}\/get.{1,3} and server response will be /Users/user/my-project/mocks/mock.xml (as you can see in config)

Serving files

You can get direct access to your mocks, just make request to http://localhost:1234/direct-mock/mock.json As you can see, you need to add route path

/direct-mock/<relative_path_to_mock>

in url. All after this route path will be relative path to your file. Here you cannot pass absolute paths, but in future maybe I'll implement absolute path support

Message broadcasting

To broadcast message you need to make request http://localhost:1234/broadcast/this-is-message So, as you can see, the same logic as above. You need to add special route path

/broadcast/<message>

and every client will get <message>. Example of client for this broadcasting I'll show below

Other settings

All these settings you can put in config file or as CLI parameters

{
  // this is all available settings

  "api": "https://api.com/get/info",
  "port": 9999,
  "socketPort": 9998,
  "https": true
  "mocks": "../mocks",
  "origin": "https://google.com",
  "referer": "https://google.com/search",
  "rules": {
    ".+qwe": "./mock.json",
    "123.123": "/Users/user/Projects/JavaScript/development-mock-server/mock.xml",
    ...
  }
}

or

developmnet-mock-server --config=./config.json --port=1234 --socketport=5432 --referer=google.com and so on

YOU MUST DEFINE RULES IN CONFIG FILE, YOU CANNOT DO THIS IN CLI

Settings Reference

  • --api or "api": – api url that this server will proxy. Default https://localhost
  • --port or "port": – port for main server. Default 1234
  • --socketport or "socketPort": – port of socket server for receiving messages via /broadcast/<message>. Default 1235
  • --https or "https": – use https or http, pass true|false. Default false. Note that for https it uses not signed certificate
  • --mocks or "mocks": – path for mocks, Default: empty line
  • --origin or "origin": – Value for Origin header. Default: "api": value
  • --Referer or "Referer": – Value for Referer header. Default: "api": value
  • "rules" – ONLY IN CONFIG FILE. Value must be object. Name of property this is RegExp pattern, property value – path to mock

Example of socket client for broadcasting

const net = require('net');
const client = new net.Socket();
client.connect(1235); // !!! `--socketport` or `"socketPort":`
client.on('data', (data) => {
   // data is Uint8Array you should convert it to string
   const strData = Buffer.from(data).toString();
});