# express-mockups-middleware

> Serve mockups on proxified requests

Latest version **1.0.2** (published 2016-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-mockups-middleware
pnpm add express-mockups-middleware
yarn add express-mockups-middleware
bun add express-mockups-middleware
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-10-11 |
| First published | 2016-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | FRIN Yvonnick |
| Maintainers | yvonnick |
| Keywords | express, mockups, middleware, js, node |

## Links

- npm: https://www.npmjs.com/package/express-mockups-middleware
- Repository: https://github.com/Zenika/express-mockups-middleware
- Homepage: https://github.com/Zenika/express-mockups-middleware#readme
- Issues: https://github.com/Zenika/express-mockups-middleware/issues
- npm.io page: https://npm.io/package/express-mockups-middleware

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-10-11
- 1.0.1 — 2016-10-11
- 1.0.0 — 2016-10-11

## README

# express-mockups-middleware

## Requirements

You will need Node v6 or later version.

## What

This is an express `middleware`.
This middleware purpose is to serve mockups on proxified HTTP requests.
This is convenient during development to separate concerns.

## Install

`npm i --save-dev express-mockups-middleware`

## Use

You should pass an array of apis as first parameter of the middleware. api objects must respect the following structure.

Property | Required | Type | Description
----------|--------|---------------|-------------
`pattern` | yes | String | A pattern that `request.url` property should match to be mocked. It will be passed as RegExp argument.
`enabled` | no | Boolean | A boolean that determine if the middleware should handle this `api`.
`headers` | no | Array of Objects | A array of objects ({key: '', value: ''}) that represents HTTP headers of the mocked `response`.
`status` | no | Number | A number that will be the `HTTP status` of the mocked `response`.
`body` | no | Function | A function that take as parameter the `request` object and should return a string that will be the `message-body` of the mocked `response`.

You can also add an optional configuration object as second parameter.

Parameter | Required | Default value | Description
----------|--------|---------------|-------------
`enabled` | no | `true` | A boolean that determine if the middleware is `enabled` or not.
`force` | no | `false` | Set to true it tells the middleware to ignore `enabled` property of `apis` objects.
`logger` | no | `empty function` | Pass your own logger function that takes a string as parameter.

The middleware call `next` function of `express` when no pattern is matched. Otherwise the `response` is instantly sent to client.

## Example

HTTP requests should be proxified otherwise the middleware won't work.

For it you could use `http-proxy-middleware`.

**server.js**
```(javascript)
import express from 'express'
import proxy from 'http-proxy-middleware'
import mockups from 'express-mockups-middleware'

const app = express()
const resources = ['Resource1','Resource2'];

app.use(mockups([
  {
    pattern: '/resources',
    body: (req) => JSON.stringify(resources)
  },
  {
    pattern: '/resources/(.*)',
    body: (req) => {
      const segments = req.url.split('/')
      return resources[segments[segments.length - 1]]
    }
  }
]));

app.use(proxy('/api', {
  target: `http://distant-backend.com`,
  logLevel: 'debug'
}));

app.listen(8080)
```

HTTP requests that match one of the patterns listed in apis parameter will be mocked but others will be sent to the proxified backend.

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