2.1.0 • Published 4 months ago

servemocks v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

servemocks

ContributingChangelogPowered by diva-e

GitHub stars

Starts a webserver which provides a REST API derived from JSON, XML and other sample files located in a specified directory.

Consider the following directory structure:

mock-api/
├── image
│   └── logo.png
├── v1
│   ├── user
│   │   ├── 1.json
│   │   ├── 2.json
|   |   └── [any].json
│   ├── user.json
│   └── user.post.json
└── v2
    ├── health.json
    └── sitemap.xml

Serving directory mock-api with servemocks would create a webserver with the folllowing http endpoints:

  • GET /image/logo.png
  • GET /v1/user
  • POST /v1/user
  • GET /v1/user/1
  • GET /v1/user/2
  • GET /v2/health
  • GET /v2/sitemap

Check the examples directory for more information about api conventions.

Starting from version 2 it is also possible to produce dynamic mock responses by using an ecmascript module as mock file. Use the file extension .mjs to enable this function. Example: examples/v3.

Getting Started

Install

# install as global package
npm install -g servemocks
# or project-local
npm add -D servemocks

Usage

servemocks <directory> [-p, --port=8080] [-c, --compact-logging]

Example:

servemocks examples/mock-api -p 5000

Programmatic Usage

Servemocks is based on express and can be mounted as a submodule inside an existing app.

import { createServeMocksExpressApp } from 'servemocks';

const mainApp = express();

const options = {
  responseDelay_ms: 100,
  // servemocks prints every endpoint it registers to the console
  //  this might bloat the console log and thus you can change this setting here
  endpointRegistrationLogging: 'compact', // default is 'verbose', use 'disabled' to not show any of those logs
  // enable javascript code to be executed from a mock file with 
  //  .mjs file extension
  //  eval can be used as alternative strategy if dynamicImport does not work
  dynamicMockResponsesMode: 'eval' // one of 'disabled', 'eval' and 'dynamicImport'
}

mainApp.use('/mock-api', createServeMocksExpressApp('examples/mock-api', options))

License

MIT

2.1.0

4 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.4.1

1 year ago

2.0.0-beta.2

1 year ago

2.0.0-beta.1

1 year ago

2.0.0-beta.0

1 year ago

2.0.0-beta.3

1 year ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago