Skip to content

Rushabh5000/http-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-mock

Zero dependencies Node License: MIT Platform

Local HTTP mock server from a mocks.json definition. Define your API routes once, mock responses with status codes, delays, headers, and path param interpolation. Zero dependencies.

Like json-server but with no dependencies and full control over responses.


Install

npm install -g http-mock

Or without installing:

npx http-mock

Quick Start

http-mock init    # Create a starter mocks.json
http-mock         # Start the server

Example Output

http-mock  mocks.json
  Server running at http://localhost:3000
  7 route(s) loaded · CORS enabled

  GET     /health           → 200
  GET     /users            → 200
  GET     /users/:id        → 200
  POST    /users            → 201 +200ms
  DELETE  /users/:id        → 204
  GET     /error            → 500
  GET     /slow             → 200 +1500ms

  6:20:13 pm  GET     /users        200  4ms
  6:20:14 pm  GET     /users/42     200  2ms
  6:20:15 pm  POST    /users        201  203ms +200ms delay
  6:20:16 pm  GET     /missing      404  1ms

mocks.json Format

{
  "port": 3000,
  "delay": 0,
  "routes": [
    {
      "method": "GET",
      "path": "/health",
      "status": 200,
      "body": { "status": "ok", "time": "{{now}}" }
    },
    {
      "method": "GET",
      "path": "/users/:id",
      "status": 200,
      "body": { "id": "{{params.id}}", "name": "User {{params.id}}" }
    },
    {
      "method": "POST",
      "path": "/users",
      "status": 201,
      "body": { "id": "123", "created": true },
      "delay": 500
    },
    {
      "method": "DELETE",
      "path": "/users/:id",
      "status": 204,
      "body": null
    }
  ]
}

Route Fields

Field Type Description
method string HTTP method: GET, POST, PUT, PATCH, DELETE, *
path string URL path. :param captures path segments
status number Response status code (default: 200)
body any Response body. null = no body
headers object Extra response headers
delay number Delay in milliseconds before responding

Template Variables

Use {{...}} in body strings:

Variable Value
{{params.id}} Path param :id
{{query.name}} Query string ?name=...
{{now}} Current ISO timestamp
{{body.email}} Field from the request body

Options

http-mock                  # Use ./mocks.json
http-mock api.json         # Use a specific file
http-mock --port 4000      # Override port
http-mock --watch          # Reload on config file change
http-mock --cors           # Enable CORS headers

License

MIT


Keywords

mock server · mock api · json-server alternative · mockoon alternative · fake api · rest mock · stub server · local api · zero dependencies · cli


Built to solve, shared to help — Rushabh Shah 🛠️✨

One of 40+ zero-dependency developer CLI tools — no node_modules, ever.