rest-server-mock

0.9.2 • Public • Published

RestServerMock

A Simple REST HTTP server that serves the configured JSON responses rest-server-mock

Currently it servers JSON responses with status code 200. Future versions will support more options including : status codes, headers, encodings, etc.

About

The motivation for implementing this module was to provide a quick and simple server to be used when developing a rest client. In that cases where the rest server has not been implemented yet, I wanted to have an HTTP server responding with some mock JSON responses, without relying on a more complex mocking framework.

Installation

npm install rest-server-mock

Configuration

The server's port and routes are configured in a JSON file:

Example file:

{

  "port": 3001

  , "routes": [

    {
      "path": "/a"
      , "type": "GET"
      , "response": {
        "field_1": "f1 get a ok"
        , "field_2": "f2 get a ok"
      }
    }

    , {
      "path": "/a"
      , "type": "POST"
      , "response": {
        "field_1": "f1 post a ok"
        , "field_2": "f2 post a ok"
      }
    }

    , {
      "path": "/a"
      , "type": "PUT"
      , "response": {
        "field_1": "f1 put a ok"
        , "field_2": "f2 put a ok"
      }
    }

    , {
      "path": "/a"
      , "type": "DELETE"
      , "response": {
        "field_1": "f1 delete a ok"
        , "field_2": "f2 delete a ok"
      }
    }

  ]

}

Usage

As a commandline tool

rest-server-mock path_to_configuration_file

As a module

// Include the module.
var restServerMock = require('rest-server-mock')
// Start the server. Returns a promise.
restServerMock('path_to_configuration_file/filename.json')
.then(function (server) {
    console.log('RestServerMock is listening on port: ', server.address().port, ' ...')
    console.log('Configuration file: ', process.argv[2])
}
, function (err) {
    console.log(err)
})
// Note that the restServerMock method returns a promise.

Running the tests

npm test

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.9.2
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.9.2
    0
  • 0.9.1
    0
  • 0.9.0
    0

Package Sidebar

Install

npm i rest-server-mock

Weekly Downloads

0

Version

0.9.2

License

MIT

Last publish

Collaborators

  • pmatzavin