Standalone mock server using Restify

npm install kodokojo-mocks@0.2.9


Kodokojo-mocks is an initiative from Kodokojo project

Standalone mocking server using Restify

Open Source Love



npm install kodokojo-mocks --save-dev


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 :

  "prefix": "api/v1",
  "path": "test/test_mocks",
  "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


Inside your module


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

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


// ... Server should be started


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

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": "{{}}",
  ... }


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);


A file based (in-memory if memoryStorage is true) key-value store is exposed as 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);

To do

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