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

kodokojo-mocks

v0.2.9

Published

Standalone mock server using Restify

Downloads

57

Readme

Kodokojo-mocks is an initiative from Kodokojo project

Standalone mocking server using Restify

Open Source Love

NPM

Install

npm install kodokojo-mocks --save-dev

or

npm install -g kodokojo-mocks

Then create a folder to store your mock files

e.g: {your_project_path}/mocks

Create a config file

e.g: {your_project_path}/mocks_config.json

Config file example :

{
  "port":8075,
  "logs":false,
  "prefix": "api/v1",
  "path": "test/test_mocks",
  "routes":[
    {"path":"/user","method":"POST","mockType":"raw","serve":"0821b5c16a367e5df4044b183af3f0d18235d832"},
    {"path":"/user/:id","method":"GET","mockType":"file","serve":"user.get.json"},
    {"path":"/user","method":"PATCH","mockType":"func","serve":"user.patch.js"},
    {"path":"/auth/:token","method":"GET","mockType":"func","serve":"auth.get.js"},
    {"path":"/visits","method":"GET","mockType":"func","serve":"visits.get.js"}
  ],
  "memoryStorage": false,
  "persistStorage": true
}

port (default: 8080) : Bind server to the specified port (eg: 8080) logs (default: true) : Enable request and error logging prefix (default: "") : Prefix path (eg: http://localhost:8080 /api/v1/...) path (default: "") : Relative path to mocks folder (from your project root folder, where your package.json was created) routes (default: []) : An array containing the routes you want to mock. ExpressJs format for parameters and paths memoryStorage (default: false) : Use memory only storage persistStorage (default: false) : Previous storage file is used if memoryStorage is false else create a new empty storage file

Usage

Inside your module

Start

var MockServer = require('kodokojo-mocks');

var mockServer = new MockServer(__dirname+"/mocks_config.json");
mockServer.start().then(function(state){
  console.log(state); // Should display : { ready: true }
  console.log(server.config); // you can access to parsed configuration
});

Stop

// ... Server should be started
mockServer.stop();

CLI

Npm script

To run as a npm script. Add the CLI in your package.json replace {config_file_path} by your config file path.

{ ...
  "scripts": {
    "mockserver": "./node_modules/kodokojo-mocks/bin/mockserver.js {config_file_path}"
  }
... }

Global installation

npm install -g kodokojo-mocks
Usage

replace {config_file_path} by your config file path.

mockserver {config_file_path}

Have a look at ./test/index.js to see some examples.

Create your mocks

There is 3 ways to serve a mock : as you can see in the config file above, every mock have a type ("mockType" property) so you can define how the mock will be loaded.

Raw data

Return the value that you specified in your config file ("serve" property)

{... "mockType":"raw","serve":"[value_to_serve]"}

Json file content

Return the content of the given json file

{... "mockType":"file","serve":"[file_name]"}

The mock file should be a standard json file. You can write data from the incoming request content inside your json file by using mustach syntax, eg :

{ ...
  "identifier": "{{req.id}}",
  ... }

{{req.NAME_OF_PROPERTY_FROM_REQ_OBJECT}}

ExpressJs controller

You can use a classic ExpressJs controller to serve your data, it's allow you to write some custom/business logic as a mock, eg :

{... "mockType":"func","serve":"[file_name]"}

Controller example:

exports.controller = function(req, res, next, server) {
  // Your code here ...
  // Classic ExpressJs api below
  res.contentType = "application/json";
  res.send(200, data_to_serve);
  next();
};

Persistence

A file based (in-memory if memoryStorage is true) key-value store is exposed as server.store that can be used to store and retrieve data in a custom controller.

exports.controller = function(req, res, next, server) {
  store.get('visits', function (err, value) {
    // visits = 0
    store.put('visits', value+1, function (err) {
        // visits = 1
    });
  });

  res.contentType = "application/json";
  res.send(200, data_to_serve);
  next();
};

To do

Enhanced logs Rename type 'file' to 'json' rename type 'func' to 'controller' Npm script