# @jarvis-catalyst/mockserver

> simple mock server

Latest version **1.0.14** (published 2020-12-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install @jarvis-catalyst/mockserver
pnpm add @jarvis-catalyst/mockserver
yarn add @jarvis-catalyst/mockserver
bun add @jarvis-catalyst/mockserver
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.14 |
| Published | 2020-12-01 |
| First published | 2020-10-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 21 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ceyakimov, cerojasr, maxvega |

## Links

- npm: https://www.npmjs.com/package/@jarvis-catalyst/mockserver
- npm.io page: https://npm.io/package/@jarvis-catalyst/mockserver

## Recent versions

- 1.0.14 (latest) — 2020-12-01
- 1.0.13 — 2020-11-18
- 1.0.12 — 2020-10-14
- 1.0.11 — 2020-10-08
- 1.0.10 — 2020-10-08
- 1.0.9 — 2020-10-08
- 1.0.8 — 2020-10-08
- 1.0.7 — 2020-10-07
- 1.0.6 — 2020-10-07

## README

# Jarvis Catalyst Mock Server

Simple mock server without dependencies,
zero configuration and super easy ussing

<p align="center">
  <a href="https://www.npmjs.com/package/@jarvis-catalyst/mockserver"><img src="https://img.shields.io/npm/v/@jarvis-catalyst/mockserver?style=flat-square"></a>
  <a href="https://www.npmjs.com/package/@jarvis-catalyst/mockserver"><img src="https://img.shields.io/npm/dm/@jarvis-catalyst/mockserver?style=flat-square"></a>
</p>

## How to use

this library creates a folder `service` and `static`

- install this package `npm i @jarvis-catalyst/mockserver`
- create a folder `mockserver` at the root of your project
- create into `mockserver` folder a `index.js` file
- add this code

```js
const mock = require("@jarvis-catalyst/mockserver");
mock.default({
  dir: __dirname,
});
```

- add the task to your `package.json`

```json
"devserver": "node ./mockserver/index.js"
```

## Props

- `port`: defines the port where the server starts (default: 4000)
- `dir`: define where services are scanned
- `delayResponse`: defines the delay time when the server responds (default: 100ms)

## Create a Endpoint

- create your services into `services` folder
- it is recommended to use the following name structure
  `{verb}-{path}-your-{endpint}.js`

Example:

```js
// for a service such that
module.exports = () => ({
  path: "/indexes/v1/products/facets",
  method: "GET",
  response: (req, res, querystring, data, params) => ({
    data: {
      data: {},
    },
    status: 200,
  }),
  delay: 1000,
});

// the file name will be
// get-indexes-v1-products-facets.js
```

### props your endpoints

```js
// get_hello-world.js
const mockAsync = async () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Hello World");
    }, 100);
  });
};

module.exports = () => ({
  path: "/hello-world/:id", // :id is a varaible param
  method: "GET", // http verb
  /**
   * @pamam {IncomingMessage} res
   * @param {ServerResponse} res
   * @param {any} querystring - object /page?12 > { page: 12}
   * @param {any} data - object boby
   * @param {string[]} params - array http://foo.com/path/:id/:name > [12, 'jhon']
   */
  response: async (req, res, querystring, data, params) => {
    const message = await mockAsync();
    return {
      // data represent a return body
      data: {
        id: params[0], // is equal to value into :id
        message,
      },
      status: 200, // http status code
    };
  },
  delay: 500, // define delay this endpoint
});
```

---
_Source: https://npm.io/package/@jarvis-catalyst/mockserver · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
