# static-api-mock

> Simple static mock JSON API Server

Latest version **0.0.2** (published 2017-10-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install static-api-mock
pnpm add static-api-mock
yarn add static-api-mock
bun add static-api-mock
```

Provides the command `static-api-mock`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2017-10-26 |
| First published | 2017-10-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| Author | Levi Putna |
| Maintainers | levi-putna |

## Links

- npm: https://www.npmjs.com/package/static-api-mock
- npm.io page: https://npm.io/package/static-api-mock

## Dependencies (10)

- [fs](https://npm.io/package/fs.md) 0.0.2
- [ejs](https://npm.io/package/ejs.md) ^2.4.2
- [cors](https://npm.io/package/cors.md) ^2.7.1
- [path](https://npm.io/package/path.md) ^0.12.7
- [chalk](https://npm.io/package/chalk.md) ^1.1.3
- [yargs](https://npm.io/package/yargs.md) ^4.7.1
- [lodash](https://npm.io/package/lodash.md) ^4.13.1
- [moment](https://npm.io/package/moment.md) ^2.19.1
- [express](https://npm.io/package/express.md) ^4.14.0
- [server-destroy](https://npm.io/package/server-destroy.md) ^1.0.1

## Recent versions

- 0.0.2 (latest) — 2017-10-26
- 0.0.1 — 2017-10-26

## README

# Simple Static Mock Json Server

Create a simple mock JSON API server using a single json file to use when developing or automated testing against an predictable response.

This service extends the work by [@ajoelp](https://www.npmjs.com/~ajoelp) on the [mock-json-server](https://www.npmjs.com/package/mock-json-server) to add support to allow for customisation of the response headers and status code.

##Installation

To install you need to have NodeJS and NPM installed on your system [https://nodejs.org/en/](https://nodejs.org/en/)
Install the mock-json-server package by running `yarn add static-api-mock --dev`

**Thats it!**

## Example

To run the server adding `"start:mock": "static-api-mock db.json --port=3000"` to your package.json scripts.

```json
"scripts": {
    "start": "echo \"Some server start script\" && exit 1",
    "test": "echo \"Some test start script\" && exit 1",
    "start:mock": "mock-json-server db.json --port=3000"
  }
```

then run `yarn start:mock` this starts a server on http://localhost:3000/

## Config

You need to specify a config file (defaults to `db.json`). Use the `db.json` to mock out the desired responses. The file uses the following format.

```json
{
  "path": {

    "request method": {
      "status": 200,
      "header": {
        "header name": "header value"
      },
      "body": {
        "json":"body content"
      }
    }
  }
}
```

### Example db.json

```json
{
  "/user": {

    "get": {
      "status": 200,
      "header": {
        "Authorization": "Bearer 123ABC"
      },
      "body": [{
        "id": 1,
        "given_name": "Jo",
        "family_name": "Active",
        "active": true
      },{
        "id": 2,
        "given_name": "Sam",
        "family_name": "Inactive",
        "active": false
      }]
    },

    "put": {
      "status": 200,
      "body": {
        "id": 3,
        "given_name": "New",
        "family_name": "User",
        "active": true
      }
    }
  },

  "/user/1": {

    "get": {
      "status": 200,
      "header": {
        "example": "123ABC"
      },
      "body": {
        "id": 1,
        "given_name": "Jo",
        "family_name": "Active",
        "active": true
      }
    },

    "post": {
      "status": 200,
      "body": {
        "id": 1,
        "given_name": "Jo",
        "family_name": "Update",
        "active": false
      }
    },

    "delete": {
      "status": 201
    }

  }
}
```

A Get Request to http://localhost:8000/user will return.

```json
    [{
      "id": 1,
      "given_name": "Jo",
      "family_name": "Active",
      "active": true
    },{
      "id": 2,
      "given_name": "Sam",
      "family_name": "Inactive",
      "active": false
    }]
```

A Post Request to http://localhost:8000/user will return.

```json
    {
      "id": 3,
      "given_name": "New",
      "family_name": "User",
      "active": true
    }
```

---
_Source: https://npm.io/package/static-api-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
