# express-middleware-mock

> Middleware to mock requests using jsons and semantic folders on express

Latest version **0.6.2** (published 2025-05-30) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2025-05-30 |
| First published | 2023-10-13 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 10 |
| Unpacked size | 35.3 KB |
| Known vulnerabilities | 0 (+3 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Renato Miawaki |
| Maintainers | reytuty |

## Links

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

## Dependencies (10)

- [fs](https://npm.io/package/fs.md) ^0.0.1-security
- [node](https://npm.io/package/node.md) ^20.7.0
- [path](https://npm.io/package/path.md) ^0.12.7
- [uuid](https://npm.io/package/uuid.md) ^9.0.1
- [express](https://npm.io/package/express.md) ^4.18.2
- [slugify](https://npm.io/package/slugify.md) ^1.6.6
- [faker-br](https://npm.io/package/faker-br.md) ^0.4.1
- [@types/uuid](https://npm.io/package/@types/uuid.md) ^9.0.5
- [@faker-js/faker](https://npm.io/package/@faker-js/faker.md) ^7.6.0
- [mongo-base-crud](https://npm.io/package/mongo-base-crud.md) ^0.1.3

## Recent versions

- 0.6.2 (latest) — 2025-05-30
- 0.6.1 — 2024-08-22
- 0.6.0 — 2024-08-22
- 0.5.13 — 2024-08-21
- 0.5.12 — 2024-08-19
- 0.5.11 — 2024-08-06
- 0.5.10 — 2024-08-06
- 0.5.9 — 2024-08-06
- 0.5.8 — 2024-08-06
- 0.5.7 — 2024-08-06
- 0.5.6 — 2024-08-06
- 0.5.5 — 2024-08-06
- 0.5.4 — 2024-05-15
- 0.5.3 — 2024-05-14
- 0.5.2 — 2024-05-09
- … 15 more at https://npm.io/package/express-middleware-mock/versions

## README

# express-middleware-mock

Middleware to mock requests using jsons and semantic folders on express

## Installation

```
npm i express-middleware-mock
```

## Usage

```
import express, { Request, Response, NextFunction } from "express";
import cors from "cors"; //optional
import mockJson from "express-middleware-mock";

const USE_MOCK = process.env.USE_MOCK || false;
const MOCK_FOLDER = process.env.MOCK_FOLDER || './jsons';

const app = express();
app.use(cors());
app.use(express.json());

//here your router
const router = express.Router();
router.get(
  "/my/router/example",
  async (req: Request, res: Response) => {
    res.status(200).send({
      success: true,
      messages: ["ok"]
    })
  }
);

if (USE_MOCK) {
  //if enter here
  app.use(mockJson(MOCK_FOLDER));
}

```

## Creating Requests and Responses

To create requests and responses you need to create a folder with semantic name as a request path, and put two files `request.json` and `response.json` inside OR you can create just a single file named `redirect.json`

### request.json file

Request file simbolise what do you need to receive as query params, body or parameter, and the syntax need to be:

```
{
  "params": {
    "someValueFromQueryParams": true
  },
  "headers":{
    "someHeadersParamsHere": true,
    "bearer": true
  },
  "body":{
    "myPostParamHere": true
  },
  "query":{
    "myQueryParamHere": true
  }
}
```

- `body`: for params inside a body request (not work for GET methods)
- `query`: for params on url, after ? simble, like `?skip=0`
- `params`: it is not possible to check url params yet
- `headers`: Set required variables from headers

Examples:

To recieve userId and userEmail on post method

```
{
    "body": {
        "userId": true,
        "userEmail": true
    }
}
```

To recieve skip and limit on get method as url params, but not optionaly, and category need to be setted

```
{
    "query": {
        "skip": false,
        "limit": false,
        "category": true
    }
}
```

```
{
    "headers": {
        "bearer": true
    }
}
```

### response.json file

Just need to be a valid json file and they will be used in body result.

The middleware will be put automatic properties to indentify this result as a fake result

```
{
  isRedirected: true,
  fakeResult: true
}
```

And if you put some values that exists on faker as a method or inside a objectHandler as a methodName, the value will be put with a method result.

You also can create a response using a list response pattern like this:

| tip: If you use this struture to result list, the fake result layer will simulate a list of result

```
{
  "total": 405,
  "skiped": 0,
  "limited": 10,
  "list": [
    {
      "id": "uuid",
      "name": "name.fullName",
      "description": "lorem",
      "otherThing": "{query.skip}"
    }
  ]
}
```

And they will be simulate 405 results on database to create a pagination system. Notice that you just need to put 1 object inside an array and they will use this as a pattern to create others.

You can response asingle result:

```
{
  "id": "uuid",
  "category": "that|this|other"
}
```

| tips: if you put pipe `|` to split string, they will random someone to result

### Create your own fake result

You can create your own result handler

```
const myHandler = {
  mySecret:()=>{
    return "my secret word here"
  }
}

if (USE_MOCK) {
  //using your own handler
  app.use(mockJson(MOCK_FOLDER, myHandler));
}
```

| tip: Use relative path with `./` or absolute path

And now you can use:

```
{
  "category": "that|this|other",
  "test" : "mySecret"
}
```

The result will be:

```
{
  "category": "other",
  "test" : "my secret word here"
}
```

### Folder Exemple

```
.
 /mock
    /test
      /a
        request.json
        response.json
    /my
      /router
        /example
          request.json
          response.json
        /example-2
          request.json
          response.json
          errors.json
    /other
      /[paramName] <- this variable goes to params variable as paramName
        /test
    /api
      /users
        /list
          redirect.json
```

We have 3 examples here and all of them inside a 'jsons' folder.

- `test/a` using request/response files
- `my/router/example` using request/response files BUT using a existed router path
- `my/router/example-2` using request/response and chance to result error
- `api/users/list` using redirect.json file

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