# lightweight-mock-server

> A lightweight and simple but flexible mock server.

Latest version **0.9.6** (published 2026-06-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install lightweight-mock-server
pnpm add lightweight-mock-server
yarn add lightweight-mock-server
bun add lightweight-mock-server
```

Provides the command `lightweight-mock-server`.

## Health

**Score 55/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.9.6 |
| Published | 2026-06-03 |
| First published | 2021-10-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=18.16.0 |
| Dependencies | 11 |
| Unpacked size | 30.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Christoph Erdmann |
| Maintainers | mcsodbrenner |

## Links

- npm: https://www.npmjs.com/package/lightweight-mock-server
- Repository: https://github.com/McSodbrenner/lightweight-mock-server
- Homepage: https://github.com/McSodbrenner/lightweight-mock-server#readme
- Issues: https://github.com/McSodbrenner/lightweight-mock-server/issues
- npm.io page: https://npm.io/package/lightweight-mock-server

## Dependencies (11)

- [cors](https://npm.io/package/cors.md) ^2.8.6
- [axios](https://npm.io/package/axios.md) ^1.17.0
- [marked](https://npm.io/package/marked.md) ^18.0.4
- [express](https://npm.io/package/express.md) ^5.2.1
- [nodemon](https://npm.io/package/nodemon.md) ^3.1.14
- [bare-css](https://npm.io/package/bare-css.md) ^2.0.3
- [commander](https://npm.io/package/commander.md) ^15.0.0
- [highlight.js](https://npm.io/package/highlight.js.md) ^11.11.1
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.7
- [express-session](https://npm.io/package/express-session.md) ^1.19.0
- [marked-highlight](https://npm.io/package/marked-highlight.md) ^2.2.4

## Recent versions

- 0.9.6 (latest) — 2026-06-03
- 0.8.5 — 2021-10-09
- 0.8.4 — 2021-10-07
- 0.8.3 — 2021-10-07
- 0.8.2 — 2021-10-07
- 0.8.1 — 2021-10-07
- 0.8.0 — 2021-10-07

## README

# Lightweight Mock Server

A lightweight and simple but flexible mock server for [node](http://nodejs.org) which is based on the [Express](https://expressjs.com/) framework and just extended with some convenience features.

Unlike other solutions, the endpoints are specified programmatically rather than descriptively, which gives you more freedom to design your endpoints (e.g. sorting or filtering of entries can actually be implemented).


## Quick Start

Install it as local dev dependency for your project:  
`$ npm i --save-dev lightweight-mock-server`

Start the server:  
`$ npx lightweight-mock-server`  
or better (to automatically restart the server on changes to your API)  
`$ npx nodemon --exec npx lightweight-mock-server`

View this README at: http://localhost:3030/-


## Configuration

You can pass some arguments to adjust the behaviour of the mock server. For example pass the port if you want a port different from 3030.  
`$ npx lightweight-mock-server --port=3000`

See all parameters via:  
`$ npx lightweight-mock-server --help`

At the moment there are three parameters:

| Parameter | Abbreviation | Description
|-----------|------------|-----
| `--port` | `-p` | Port of the mock server. (default: 3030)
| `--entrypoint` | `-e` | Path to the entrypoint file with your API definitions. (default: `./mock-data/api.js`)
| `--build` | `-b` | Build a static representation of the mock definitions.
| `--help` | `-h` | Display help.


## Create your own mock environment

You have to create a file an entrypoint file (default: `./mock-data/api.js`) for your mock definitions.
This file has to export a default function and gets two parameters passed:

| Parameter | Description
|-----------|------------
| **app** | A standard "Express" app object you can use to define your routes and your API functionality.
| **env** | An object with some variables and helpers.
| *env*.data | The path to the entrypoint dir.
| *env*.args | The arguments which were passed to the CLI command.
| *env*.express | The [Express](http://expressjs.com/) object. Useful to instantiate a Router object.
| *env*.axios | An axios instance.
| *env*.session | An [express-session](https://www.npmjs.com/package/express-session) object to be able to handle e.g. authentication.
| *env*.saveRoute | ...


### Commented example  

```js
const api = function(app, env) {
	// if you want all your routes to be available via /api/* use this,
	// otherwise you would have to use app.* instead of router.* for all route definitions
	const router = env.express.Router()
	app.use('/api', router)
	
	// a simple possibility to delay all requests to simulate a slow network
	router.use((req, res, next) => {
		setTimeout(next, env.args.build ? 0 : 1000)
	})

	// an example which renders your README.md with the docs for your API
	// the command "sendMarkdown" is an extension for the response object by lightweight-mock-server
	// endpoint available via /api
	app.get('/', (req, res) => {
		res.render('README.md')
	})    

	// simple response of json data
	// endpoint available via /api/json
	router.get('/json', (req, res) => {
		res.json(env)
	}) 

	// if your response doesn't have to be dynamic you can also just return a file you've prepared
	// endpoint available via /api/colors
	router.get('/colors', (req, res) => {
		res.sendFile('colors.json', { root: env.data })
	})    

	// session handling is already included (useful to fake a simple login system)
	// endpoint available via 
	// /api/user
	// /api/user?action=login
	// /api/user?action=logout
	router.get('/user', (req, res) => {
		if (!req.query.action) {
			res.send('logged in: ' + (env.session.loggedin ? 'true' : 'false'))
		}
		else if (req.query.action === 'login') {
			env.session.loggedin = true
			res.send('logged in')
		}
		else if (req.query.action === 'logout') {
			env.session.loggedin = false
			res.send('logged out')
		}
	})
}

export {
	api as default,
};
```


## Convenience

All routes starting with `/-/` are endpoints with convenience functionality.


| Endpoint | Description |
|-----------|-----------|
| `/-` or `/-/` | Shows this README. |
| `/-/500`<br />`/-/404`<br />`/-/403`<br />`/-/xxx` | Returns the HTTP error message with the corresponding HTTP status code. |
| `/-/mirror` | Returns a JSON object with several data you can use to analyze your request. |

In addition, a template engine was registered, which renders Markdown files desirably. So Markdown will be rendered by [marked](https://marked.js.org/) and styled by [bare.css](https://barecss.com/).

```js
app.get('/', (req, res) => {
	res.render('README.md')
}) 
```


## Static build generation

If you have a static build of your clickdummy it could make sense to have a static build of your mocked api too to be able to upload your complete clickdummy to a webspace which is only capable of delivering static ressources. For this reason it's possible to save static files of your API endpoints. As your API endpoints can be very individually programmed it is not possible to simple generate the static files without your assistance.

For this to work you have to export a function named `build` from your entrypoint file (default: `./mock-data/api.js`).


### Example

```js
const build = function(_app, env) {
	return [
		env.saveRoute('get', ['/'], 'dist/api/README.htm'),
		env.saveRoute('get', ['/api/colors'], 'dist/api/colors'),
		env.saveRoute('get', ['/api/faker'], 'dist/api/faker'),
	]
}

export {
	api as default,
	build,
};
```

This function has to return an array of promises. lightweight-mock-server provides a helper function called `saveRoute()` which utilizes [axios](https://axios-http.com/) to access your API. It excepts three parameters: `axiosMethod` (the method of axios that will be called), `axiosParams` (parameters that will be passed to axios via apply; so you are free to finetune your HTTP request) and `filePath` (the path the result of the HTTP request will be saved to).

To generate your static build, simply call:  
`$ npx lightweight-mock-server --build`

## License

[ISC License (ISC)](https://opensource.org/licenses/ISC)  
Copyright 2021 Christoph Erdmann

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

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