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

@codebalancers/mock-server

v1.0.12

Published

Mock server written in Typescript

Downloads

6

Readme

cb-mock-server

The mock-server is able to serve the contents of two distinct directories:

  • static: contains static files and sub-folders that are returned directly and are separated by http request methods (GET, POST, ...)
  • dynamic: contains java script files that are executed

File in the static directory MAY have a .json extension. Files in the dynamic directory have a .js extensions.

Considering the following directory structure:

/static/
  GET/
    files/
      test.jpg
      README
    users/
      index.json
      234.json
  POST/
    users/
      234.json
/dynamic/
  users/
    567.js
  groups/
    index.js
  misc/
    readme.txt.js
  • GET /users/ will return the /static/users/index.json file
  • GET /files/test.jpg will return the /static/GET/files/test.jpg file
  • GET /files/README will return the /static/GET/files/README file
  • GET /users/234 will return the /static/GET/users/234.json file
  • POST /users/234 will return the /static/POST/users/234.json file
  • POST /users/567 will execute the /dynamic/users/567.js script
  • GET /users/567 will execute the /dynamic/users/567.js script
  • DELETE /groups will execute the /dynamic/groups/index.js script
  • GET /misc/readme.txt will execute the /dynamic/misc/readme.txt.js script

Example scripts

Scripts must export a handle function that takes two parameters, a request and a response parameter.

exports.handle = function (req, res) {
  var password = req.body;

  if (password !== 'secret') {
    res.status(403).send('wrong password');
  } else {
    res.send({result: 'ok'});
  }
};

Install and setup

yarn add cb-mock-server

In package.json add e.g.

"scripts": {
  "mock:serve": "cb-mock-server --dynamic=mocks/dynamic --static=mocks/static --port=3000"
}

The mock server accepts the following parameters:

  • --dynamic=: relative path to directory with dynamic scripts
  • --static=: relative path to directory with static files
  • --port=: server port (default is 3000)

Watch & Reload

If a script is changed (and was already used by the server), the server must be restarted. This can be automated with nodemon.

First install nodemon:

yarn add nodemon

Then change the start script in package.json to e.g.:

"mock:serve": "nodemon --watch mocks/dynamic --exec cb-mock-server --dynamic=mocks/dynamic --static=mocks/static --port=3000"

Use cases

Return a different http code than 200

Create a .js script and specify the response code.