Mock Server
Get a full REST API with
zero coding in
less than 30 seconds (seriously)
Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.
Now also available as a VSCodeExtension
thinker.mock-server.
Table of contents
Details
Getting started
Install Mock Server
npm install -g @r35007/mock-server @r35007/mock-server-cli
Run init command to create sample server files.
mock-server --init
Start Mock Server
mock-server
Now if we go to http://localhost:3000/posts, we'll get
[{ "id": 1, "title": "mock-server", "author": "r35007" }]
CLI Usage
$ mock-server --help
mock-server [options] <source>
Options:
-c, --config Path to config file [string] [default: "mock-server.json"]
-P, --port Set port [number] [default: 3000]
-H, --host Set host [string] [default: "localhost"]
-r, --root Set root directory. [string] [default: "./"]
-s, --static Set static files directory [string] [default: "./public"]
-b, --base Set base route path [string] [default: ""]
--db Path to database file [string]
--middlewares, --md Path to middlewares file [string]
--injectors, --in Path to Injectors file [string]
--store, --st Path to Store file [string]
--rewriters, --rw Path to Rewriter file [string]
--id Set database id property [string] [default: "id"]
--init Create a sample server files [boolean] [default: false]
--dbMode, --dm Set Db mode [string] [default: "mock"] [choices: "mock", "dev", "multi"]
--snapshots, --ss Set snapshots directory [string] [default: "./"]
--reverse, --rv Generate Route in revere order [boolean] [default: false]
--readOnly, --ro Allow only GET requests [boolean] [default: false]
--noCors, --nc Disable Cross-Origin Resource Sharing [boolean] [default: false]
--noGzip, --ng Disable GZIP Content-Encoding [boolean] [default: false]
--noCache, --nch Disable Caching [boolean] [default: true]
--bodyParser, --bp Enable body-parser [boolean] [default: true]
--cookieParser, --cp Enable cookie-parser [boolean] [default: true]
-l, --logger Enable logger [boolean] [default: true]
--log Enable Setter Logs [boolean] [default: false]
-w, --watch Watch for changes [boolean] [default: false]
-q, --quiet Prevent console logs [boolean] [default: false]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Examples:
mock-server --init
mock-server db.json
mock-server --watch db.json
mock-server http://jsonplaceholder.typicode.com/db
https://r35007.github.io/Mock-Server/
Using JS Module
Create server/index.js
File
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname });
mockServer.launchServer('./db.json');
or
const { MockServer, watcher, chalk } = require('@r35007/mock-server');
const config = require('../.mockserverrc.js');
const mockServer = MockServer.Create(config);
const startServer = async () => {
const app = mockServer.app;
mockServer.setData({
injectors: config.injectors,
middlewares: config.middlewares,
store: config.store,
});
const rewriter = mockServer.rewriter(config.rewriters);
app.use(rewriter);
const defaultsMiddlewares = mockServer.defaults();
app.use(defaultsMiddlewares);
const resources = mockServer.resources(config.db);
app.use(resources.router);
const homePage = mockServer.homePage();
app.use(homePage);
app.use(mockServer.pageNotFound);
app.use(mockServer.errorHandler);
await mockServer.startServer();
};
startServer().then(() => {
const watch = watcher.watch(mockServer.config.root);
watch.on('change', async () => {
process.stdout.write(chalk.yellowBright(changedPath) + chalk.gray(' has changed, reloading...\n'));
if (!mockServer.server) return;
await MockServer.Destroy(mockServer).then(() => startServer());
});
});
Now go to terminal and type the following command to start the Mock Server.
node server
For more api reference please click here,
Database
We can add the database in three ways. Using setData
, setDb
or resources.
The easy and efficient way to add the database is using resources
.
Create db.json
{
"posts": [{ "id": 1, "title": "mock-server", "author": "r35007" }],
"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],
"profile": { "name": "r35007" }
}
Now in server.js
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname });
const app = mockServer.app;
const resources = mockServer.resources('./db.json');
app.use(resources.router);
const newDb = {
users: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
],
profile: { name: 'foo' },
};
const newResource = mockServer.resources(newDb);
app.use(resources.router);
mockServer.startServer();
Database with Custom Options
We can create database with custom injectors, middlewares, dbMode etc..
For Example: server.js
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname });
const app = mockServer.app;
const todos = {
todos: [
{ id: 1, title: 'Todo 1', completed: false },
{ id: 2, title: 'Todo 2', completed: false },
],
};
const injectors = [{ routes: '/(.*)', delay: 1000, middlewares: ['log'] }];
const middlewares = {
log: (req, res) => {
console.log(req.url);
res.send(res.locals.data);
},
};
const todosResource = mockServer.resources(todos, {
injectors,
middlewares,
});
app.use(todosResource.router);
mockServer.startServer();
Please check resources api for more custom option reference.
Database with Create method
We can create database using create method by resources.
For Example: server.js
const { MockServer } = require("@r35007/mock-server");
const mockServer = MockServer.Create({ root: __dirname });
const app = mockServer.app;
const resources = mockServer.resources();
resources.create("/todos", (req, res, next) => {next()})
.mock({"userId": 1 "id": 1 "title": "Marvel" "completed": false})
.delay(1000)
.done();
resources.create("/photos")
.fetch("https://jsonplaceholder.typicode.com/photos")
.fetchCount(3);
.done();
const dbMode = 'multi';
resources.create("/users")
.send("https://jsonplaceholder.typicode.com/users", dbMode)
.done();
resources.create("/posts")
.send({ userId: 1 id: 1 title: "Marvel" body: "My First post"})
.done();
app.use(resources.router);
mockServer.startServer();
All available methods to create a route.
server.js
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname });
const app = mockServer.app;
const resources = mockServer.resources();
middlewares = [
(req, res, next) => {
next();
},
(req, res, next) => {
next();
},
];
const db = resources
.create('/todos', ...middlewares)
.send('My Response', mockServer.config.dbMode)
.id('todos')
.description('todos route')
.mock({ userId: 1, id: 1, title: 'Marvel', completed: false })
.fetch('https://jsonplaceholder.typicode.com/todos')
.mockFirst(false)
.statusCode(200)
.delay(0)
.fetchCount(1)
.skipFetchError(false)
.directUse(false)
.headers({})
.done({ log: true });
console.log(db);
app.use(resources.router);
mockServer.startServer();
Please check resources api for more custom option reference.
Middlewares
We can use the middlewares by setting it using setData
or setMiddlewares
.
For Example: middlewares.js
const isAuthorized = (_req) => true;
const auth = (req, res, next) => {
if (isAuthorized(req)) return next();
res.sendStatus(401);
};
const DataWrapper = (req, res, next) => {
res.locals.data = {
status: 'Success',
message: 'Retrieved Successfully',
result: res.locals.data,
};
next();
};
module.exports = (mockServer) => {
const { app, routes, data, getDb, getStore } = mockServer || {};
const { config, db, injectors, middlewares, rewriters, store } = data || {};
return {
_globals: [auth],
DataWrapper,
};
};
server.js
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname });
const app = mockServer.app;
const defaults = mockServer.defaults();
app.use(defaults);
mockServer.setMiddlewares('./middlewares.js');
app.use(mockServer.middlewares._globals);
const resources = mockServer.resources({
'/fetch/users1/customMiddleware': {
_config: true,
fetch: 'http://jsonplaceholder.typicode.com/users',
middlewares: ['DataWrapper'],
},
});
app.use(resources.router);
Please check Setters for more api reference.
Direct Middleware Use
Usually all the middlewares in the route will be wrapped by some helper middlewares to set delay, get fetch data, set locals etc..
If we wish to provide a middlewares without any wrappers set directUse
to true
.
For Example: db.js
const db = {
"/static/app1": express.static("./path/to/build/folder/app1")
"/static/app2": { _config: true, middlewares: express.static("./path/to/build/folder/app2"), directUse: true }
"/fetch/users": {
_config: true,
fetch: "http://jsonplaceholder.typicode.com/users",
middlewares:(req, res) => { res.send("custom response")},
directUse: true
}
}
Note: /fetch/users
wont work since it wont be wrapped by helper middlewares and so no other route config would work except the given middleware if provided.
Injectors
Here we are explicitly injecting delay
, middlewares
, statusCode
to the /posts
route.
We can add any route configs to a specific or to a group of routes using Injectors.
- Injectors use
path-to-regexp
package for route pattern recognition. - Click path-to-regexp for more details.
For Example : injectors.json
[
{ "routes": "/posts", "delay": 2000 } // Adds delay of 2 seconds only to /posts route
{ "routes": "/comments/:id", "statusCode": 500 } // Adds statusCode of 500 for all the route that matches the pattern /comments/:id
]
server.js
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create({ root: __dirname, dbMode: 'fetch' });
const resources = mockServer.resources({
'/posts': 'http://jsonplaceholder.typicode.com/posts',
'/comments/1': 'http://jsonplaceholder.typicode.com/comments/1',
'/comments/2': 'http://jsonplaceholder.typicode.com/comments/2',
});
const app = mockServer.app;
mockServer.setInjectors('./injectors.json');
app.use(mockServer.middlewares._globals);
app.use(resources.router);
Please check Setters for more api reference.
Override Existing Route Configs
Setting override
flag to true helps to override the existing config of that route.
For example :
injectors.json
[
{
"routes": ["/injectors/2"],
"override": true,
"mock": "This data is injected using the injectors by matching the pattern '/injectors/2'."
},
{
"routes": ["/injectors/:id"],
"override": true,
"exact": true,
"statusCode": 200,
"mock": "This data is injected using the injectors by exactly matching the route '/injectors/:id'."
},
{
"routes": ["/(.*)"],
"override": true,
"middlewares": ["...", "CustomLog"]
}
]
Note: Use ["..."]
If we want to add the existing middlewares.
Common Route Configs
Using wildcards we can set a common route configs to all the routes.
/(.*)
- matches all the routes.
For example :
injectors.json
[
{
"routes": ["/(.*)"],
"description": "This Description is injected using the injectors. Set 'Override' flag to true to override the existing config values."
},
{
"routes": ["/(.*)"],
"override": true,
"middlewares": ["...", "CustomLog"]
}
]
Make sure we give /(.*)
at the end of the injectors.json
object to set route configs to all the routes.
Store
Store used to store any values which can be used later for any purpose like response manipulation or logging etc..
We can set store values using mockServer.setStore or using mockServer.setData
Route Store
Route store helps to store any values which can be accessed on by that particular route.
This stores values cannot be able to accessed by the other routes.
Route Store can be accessed using res.locals.routeConfig.store
inside the middleware.
The middlewares _IterateRoutes
and _IterateResponse
uses the Route store to manipulate response.
Local Store
Local Store helps to store and share data between routes.
This can be accessed using res.locals.getStore()
inside the middleware.
Rewriters
Create a rewriters.json
file. Pay attention to start every route with /
.
- Rewriters use
express-urlrewrite
package to rewrite the urls. - Click here for more information about url rewrite.
rewriters.json
{
"/api/*": "/$1",
"/:resource/:id/show": "/:resource/:id",
"/posts/:category": "/posts?category=:category",
"/articles?id=:id": "/posts/:id"
}
server.js
const mockServer = MockServer.Create();
const app = mockServer.app;
const rewriters = mockServer.Rewriter('./rewriters.json');
app.use(rewriters);
Now we can access resources using additional routes.
/api/posts
/api/posts/1
/posts/1/show
/posts/javascript
/articles?id=1
Helper Middlewares
Use the helper middleware to speedup our development and for ease of access.
By default some helper middlewares used for a route.
Default Helper middlewares
const middlewares = [
_SetDelay,
_Fetch,
_SetStatusCode,
_SetHeaders,
_CrudOperation,
...userMiddlewares,
_SetStatusCode,
_SetHeaders,
_SendResponse,
];
const middlewares = withHelperWrappers(userMiddlewares);
Note: These helper middlewares will not be used if we set directUse: true
in a route config.
IterateResponse
setting middleware to _IterateResponse
helps to send we a iterate the response one after the other in the mock array for each url hit.
example:
{
"/middleware/example/_IterateResponse": {
"_config": true,
"description": "This route iterates through each data. Try to hit again to see the data change. Note: The data must be of type array",
"fetch": {
"url": "http://jsonplaceholder.typicode.com/photos"
},
"middlewares": ["_IterateResponse"]
}
}
Now go and hit http://localhost:3000/middleware/example/_IterateResponse. For each hit we will get the next object in an array from the photos data.
IterateRoutes
setting middleware to _IterateRoutes
helps to send a iterate the route one after the other in the mock array for each url hit.
example:
{
"/middleware/example/_IterateRoutes": {
"_config": true,
"description": "This route iterates through each route provide in the mock. Try to hit again to see the route change. Note: The data must be of type array",
"mock": ["/injectors/1", "/injectors/2"],
"middlewares": ["_IterateRoutes"]
},
"/injectors/1": "/injectors/1 data",
"/injectors/2": "/injectors/2 data"
}
Now go and hit http://localhost:3000/middleware/example/_IterateRoutes. For each hit the route is passed to next matching url provided in the mock list.
CrudOperations
By default handles all the crud operations of the given data.
Note: The mock must of type Array of objects and must contain a unique value of attribute id
. This id
attribute can also be changes using config.id
.
For example: config.json
{
"id": "_id"
}
Others
_Fetch
fetches data from file or url._SetDelay
sets status code from res.locals.delay
._SetStatusCode
sets status code from res.locals.statusCode
._SetHeaaders
sets headers from res.locals.headers
._SendResponse
sends the response from res.locals.data
._FetchTillData
helps to fetch the data from url until it get a success data. Once its get the data the fetch call stops and returns the existing data for other route hit._SetFetchDataToMock
sets every fetchData to Mock. This overrides the existing mock with the fetchData
._MockOnly
sends we only Mock data even if there is any fetchData
or store
data._FetchOnly
sends we only Fetch data even if there is any _mock
or store
data._ReadOnly
forbidden every Http method calls except GET
call.
App Configuration
Mock Server uses cosmiconfig
for configuration file support.
This means you can configure Mock Server
via (in order of precedence):
- A
mock-server
key in your package.json file. - A
.mock-serverrc
, .mock-serverrc.json
, .mock-server.config.json
, mock-server-config.json
file. - A
.mockserverrc
, .mockserverrc.json
, .mockserver.config.json
, mockserver-config.json
file. - A
.mock-serverrc.<js|cjs>
, .mock-server.config.<js|cjs>
, mock-server-config.<js|cjs>
file that exports an object using module.exports. - A
.mockserverrc.<js|cjs>
, .mockserver.config.<js|cjs>
, mockserver-config.<js|cjs>
file that exports an object using module.exports.
The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found.
Mock Server intentionally doesn’t support any kind of global configuration. This is to make sure that when a project is copied to another computer, Mock Server's behavior stays the same. Otherwise, Mock Server wouldn’t be able to guarantee that everybody in a team gets the same consistent results.
The options you can use in the configuration file are
JSON:
{
"port": 3000,
"host": "localhost"
}
JS:
module.exports = {
port: 3000,
host: 'localhost',
};
Complete Options:
JS:
const path = require('path');
const public = path.join(process.cwd(), 'public');
module.exports = {
port: 3000,
host: 'localhost',
root: process.cwd(),
base: '',
id: 'id',
dbMode: 'mock',
static: public,
reverse: false,
logger: true,
noCors: false,
noGzip: false,
noCache: true,
readOnly: false,
bodyParser: true,
cookieParser: true,
quiet: false,
};
dbMode
The object which has _config: true
are considered as route configs.
All other values and objects are considered as direct values.
Db mode defines on what config does the direct route value to be assigned.
The object with _config: true
will not be changed on any dbMode
.
For Example :
dbMode
is mock
- All direct values will be assigned to mock
attribute
const db = {
route1: 'My Response',
route2: { data: 'My Response' },
route3: [],
route4: { _config: true, fetch: 'path/to/file' },
};
dbMode
is fetch
- All direct values will be assigned to fetch
attribute
const db = {
route1: 'path/to/file',
route2: { url: 'path/to/file' },
route3: { _config: true, mock: 'My Response' },
};
dbMode
is multi
- Only direct string
value will be assigned to fetch
attribute. All other values will be assigned to mock
attribute
const db = {
route1: 'path/to/file',
route2: { data: 'My Response' },
route3: [],
route4: { _config: true, mock: 'My Response' },
};
Route Config
Route config helps to define a config such as delay. statuscode to a specific route.
Routes which as a object with _config: true
as considered as a route config.
interface RouteConfig {
_config?: boolean;
id?: string;
description?: string;
statusCode?: number;
headers?: object;
delay?: number;
fetch?: string | AxiosRequestConfig;
fetchCount?: number;
mock?: any;
mockFirst?: boolean;
skipFetchError?: boolean;
store?: object;
middlewares?: express.RequestHandler | Array<express.RequestHandler> | string;
fetchData?: {
status?: number;
message?: string;
isError?: boolean;
headers?: any;
response?: any;
stack?: any;
};
_isFile?: boolean;
_request?: AxiosRequestConfig;
_extension?: string;
}
Note: Object without _config: true
will be considered as a direct mock response. Please make sure we set config: true
to config the route.
Set Custom Delay
delay
helps we to set a custom delay to our routes.
{
"/customDelay": {
"_config": true,
"delay": 2000, // The delay must be in milliseconds and of type number
"description": "Note: give delay in milliseconds",
"mock": "This is response is received with a delay of 2000 milliseconds"
}
}
Now if we go to http://localhost:3000/customDelay, we'll get the response in a delay of 2 seconds.
Set Custom StatusCode
statusCode
helps we set a custom statusCode to our routes.
It must be of type number and between 100 to 600.
{
"/customStatusCode": {
"_config": true,
"statusCode": 500,
"mock": "This is response is received with a statusCode of 500"
}
}
Now if we go to http://localhost:3000/customStatusCode, we'll get the response with a 500
statusCode
Fetch File or URL
fetch
helps we get data from url.
The url can either be a http server or a local file.
Fetch File
Give a absolute or a relative path to fetch any file.
{
"/fetch/local/file": {
"_config": true,
"description": "The given fetch path will be relative to the root path given in config",
"fetch": "./data/users.json"
}
}
Note: The given path will be relative to config.root
.
Fetch Data From URL
{
"/fetch/posts/:id": {
"_config": true,
"fetch": "https://jsonplaceholder.typicode.com/posts/${req.params.id}"
}
}
Axios Fetch Request
We can also give a fetch as a axios request object with custom options.
{
"/fetch/posts/:id": {
"_config": true,
"description": "Give the `fetch` attribute as a axios request object. enclose the value with ${<variables>} to pass the req values",
"fetch": {
"method": "GET",
"url": "http://jsonplaceholder.typicode.com/posts",
"params": "${req.params.id}"
}
}
}
http://localhost:3000/fetch/posts/2.
Note: To pass any options from the route set the option value as ${<option Name>}
reserved key words :
${config}
- get all config values${req}
- get all req values
Fetch Count
In Route Config setting fetchCount
will helps to limit the number of fetch calls.
By Default the fetchCount
is set to 1
.
The fetch data will be set to fetchData
.
db.json
{
"/fetch/todos/fetchCount/3/times": {
"_config": true,
"description": "By default the fetch will be called only one time. We can limit or extend the number of fetch calls using 'fetchCount' attribute",
"fetch": "http://jsonplaceholder.typicode.com/todos",
"fetchCount": 3
},
"/fetch/albums/fetchCount/Infinite/times": {
"_config": true,
"description": "Setting 'fetchCount' to -1 time will helps to make a fetch call on each and every url hit without any limit. By This way we always get a new fresh data from the fetch url.",
"fetch": "http://jsonplaceholder.typicode.com/albums",
"fetchCount": -1
}
}
http://localhost:3000/fetch/todos/fetchCount/3/times. - Makes fetch call only for 3 times.
http://localhost:3000/fetch/todos/fetchCount/Infinite/times. - Makes fetch call Infinite times.
Skip Fetch Error
If skipFetchError
is set to true, It will skip any error in fetch call and instead of returning that fetch error it gives we the mock data.
{
"/fetch/404/skipFetchError": {
"_config": true,
"description": "Bu default fetch returns the actual error if occur. If we set `skipFetchError` flag to true. the If any error occur in fetch call it will then skips the fetch error and return we the mock data",
"fetch": "http://localhost:3000/404",
"skipFetchError": true,
"mock": "This data is returned due to some error in fetch call. We can see the error in 'fetchError' attribute",
"fetchCount": -1
}
}
Specific Middlewares
We can add n number of middleware to a route which helps we to manipulate or log the data.
Please check Middlewares fro more reference.
db.js
const db = {
'/fetch/users1/customMiddleware': {
_config: true,
fetch: 'http://jsonplaceholder.typicode.com/users',
middlewares: ['DataWrapper'],
},
'/fetch/users2/customMiddleware': {
_config: true,
fetch: 'http://jsonplaceholder.typicode.com/users',
middlewares: (req, res, next) => {
next();
},
},
};
Locals
res.locals
helps to access the current route config, fetchData
, store
etc..
Here are the available options in res.locals
interface Locals {
routePath: string;
routeConfig: RouteConfig;
data: any;
config: Config;
getStore(): object;
getDb(): object;
}
Dynamic Route Config
RouteConfigs are mutable. Means we can able to modify the routeConfigs in runtime using middleware.
For Example:
middleware.js
exports._FetchTillData = (_req, res, next) => {
const locals = res.locals;
const routeConfig = locals.routeConfig;
if (!routeConfig.fetchData) return next();
if (!routeConfig.fetchData.isError) {
routeConfig.fetchCount = 0;
} else if (routeConfig.fetchCount !== undefined && routeConfig.fetchCount == 0) {
routeConfig.fetchCount = -1;
}
next();
};
The above middleware helps to fetch the data from url until it gets a valid success response.
Home Page Routes
API
MockServer
returns the instance of the mockServer.
const { MockServer } = require('@r35007/mock-server');
const mockServer = new MockServer('./config.json');
Params
Name | Type | Required | Description |
---|
config | string/object | No | This object sets the port, host etc.. |
Create
returns the single instance of the mockServer.
const { MockServer } = require('@r35007/mock-server');
const mockServer = MockServer.Create('./config.json');
Params
Name | Type | Required | Description |
---|
config | string / object | No | This object sets the port, host etc.. |
Destroy
Destroy simply stops the server without any exception and resets the server.
returns promise
const { MockServer } = require('@r35007/mock-server');
let mockServer = MockServer.Create();
await MockServer.Destroy(mockServer);
Params
Name | Type | Required | Description |
---|
mockServer | instance | No | Instance of the MockServer to Destroy |
launchServer
It validates all the params in the MockServer, loads the resources and starts the server.
mockServer.launchServer('./db.json', {
injectors: './injectors.json',
middlewares: './middleware.js',
rewriters: './rewriters.json',
store: './store.json',
router: express.Router(),
log: false,
});
Params
Name | Type | Required | Description |
---|
db | string/object/method | No | Set db resource |
options | object | No | option to create db resource |
[options]
Name | Type | Required | Description |
---|
injectors | string/object/method | No | injectors to inject routeconfig to this db |
middlewares | string/object/method | No | middlewares of this db |
store | string/object/method | No | store of this db |
rewriters | string/object/method | No | rewriters of this db |
router | Express.Router | No | Custom Router |
log | boolean | No | If tru it logs the setter log |
rewriter
Sets the route rewrites and return the router of the rewriters;
const rewriters = mockServer.rewriter('./rewriters.json', {
log: false,
root: __dirname,
});
app.use(rewriters);
Params
Name | Type | Required | Description |
---|
rewriters | string / object | No | Give the Rewrites |
options | object | No | option to set rewriters |
[options]
Name | Type | Required | Description |
---|
root | string | No | To require rewriter file relative to this path |
log | boolean | No | If true it logs the rewriters setting log |
defaults
returns the list of default middlewares.
Also helps to host a static directory.
const defaults = mockServer.defaults({ static: './public', readOnly: true });
app.use(defaults);
- options
static
path to static fileslogger
enable logger middleware (default: true)noGzip
disable Compression (default: false)noCors
disable CORS (default: false)readOnly
accept only GET requests (default: false)bodyParser
enable body-parser middleware (default: true)cookieParser
enable cookie-parser middleware (default: true)
resources
Create db resources. It uses global injectors, middlewares and config to crete db resource.
const resources = mockServer.resources('./db.json');
app.use(resources.router);
Create db resources with custom injectors and middlewares. It won't use global injectors and middlewares.
It sets only db and not the injectors or middlewares.
const resources = mockServer.resources("./db.json", {
injectors: "./injectors.json",
middlewares: "./middleware.js",
root: __dirname,
dbMode: "mock",
reverse: false,
router: express.Router();
log: false
});
middlewares = (req, res, next) => { next() };
const db = resources
.create("/todos", middlewares)
.send("My Response", 'multi')
.id("todos")
.description("todos route")
.mock({ userId: 1, id: 1, title: "Marvel", completed: false })
.fetch("https://jsonplaceholder.typicode.com/todos")
.mockFirst(false)
.statusCode(200)
.delay(0)
.fetchCount(1)
.skipFetchError(false)
.directUse(false)
.headers({})
.done();
app.use(resources.router);
Params
Name | Type | Required | Description |
---|
db | string/object/method | No | Set db resource |
options | object | No | option to create db resource |
[options]
Name | Type | Required | Description |
---|
root | string | No | root to get db from a file |
dbMode | 'mock'/ 'fetch'/'multi' | No | dbMode to create resource |
injectors | string/object/method | No | injectors to inject routeconfig to this db |
middlewares | string/object/method | No | middlewares of this db |
reverse | boolean | No | If true it creates db in reverse order |
router | Express.Router | No | Custom Router |
log | boolean/string | No | If tru it logs the resources setting log |
homePage
Returns Mock Server Home Page router.
const homePage = mockServer.homePage();
app.use(homePage);
Params
Name | Type | Required | Description |
---|
log | boolean | No | If tru it logs the resources setting log |
startServer
Returns a Promise of Server
. - helps to start the app server externally
const server = await mockServer.startServer(3000, 'localhost');
Params
Name | Type | Required | Description |
---|
port | number | No | Set custom Port |
host | string | No | Set custom Host |
stopServer
Returns a Promise of Boolean. - helps to stop the app server externally
const isStopped = await mockServer.stopServer();
resetServer
Clears out all values and resets the server for a fresh start.
By default this method will be called on mockServer.stopServer()
method.
mockServer.resetServer();
resetDb
Returns the routes that are reset.
const routes = mockServer.resetDb();
Params
Name | Type | Required | Description |
---|
ids | string[] | No | Give List of route ids to reset |
routePaths | string[] | No | Give List of routePaths to reset |
pageNotFound
It is a middleware to handle a page not found error.
Please use it at the end of all routes.
app.use(mockServer.pageNotFound);
errorHandler
It is a middleware to handle a any error occur in server.
Please use it at the end of all routes.
app.use(mockServer.errorHandler);
Getters
const MockServer = require('@r35007/mock-server').default;
const mockServer = MockServer.Create();
const port: number | undefined;
const server: Server | undefined;
const address: string | undefined;
const listeningTo: string | undefined;
const app = mockServer.app;
const data = mockServer.data;
const db = mockServer.db;
const middleware = mockServer.middleware;
const injectors = mockServer.injectors;
const rewriters = mockServer.rewriters;
const config = mockServer.config;
const store = mockServer.store;
const initialDb = mockServer.initialDb;
const _ = MockServer._
const axios = MockServer.axios
const express = MockServer.express
const chalk = MockServer.chalk
const watcher = MockServer.watcher
const pathToRegexp = MockServer.pathToRegexp
const nanoid = MockServer.nanoid
const ora = MockServer.ora
Setters
const MockServer = require('@r35007/mock-server').default;
const mockServer = MockServer.Create();
mockServer.setData({ db, injectors, middlewares, rewriters, store, config });
mockServer.setConfig(config, { root, merge, log });
mockServer.setMiddlewares(middlewares, { root, merge, log });
mockServer.setInjectors(injectors, { root, merge, log });
mockServer.setRewriters(rewriters, { root, merge, log });
mockServer.setStore(store, { root, merge, log });
mockServer.setDb(Db, { root, merge, log, injectors, reverse, dbMode });
mockserver.setDefaults();
mockserver.setDefaultData();
mockserver.setDefaultDb();
mockserver.setDefaultInjectors();
mockserver.setDefaultMiddlewares();
mockserver.setDefaultRewriters();
mockserver.setDefaultStore();
mockserver.setDefaultConfig();
mockserver.clearServerAddress();
mockserver.setExpressApp();
Libs
Few Libraries has been exposed for ease
const {
MockServer,
express,
lodash,
nanoid,
spinner,
pathToRegexp,
chalk,
axios,
watcher,
} = require('@r35007/mock-server');
Validators
These helps to return a valid data from provided file path or object.
const {
getValidDb,
getValidMiddlewares,
getValidInjectors,
getValidRewriters,
getValidConfig,
getValidStore,
getValidRouteConfig,
} = require('@r35007/mock-server/dist/utils/validators');
const options = {
reverse: true,
dbMode: 'fetch',
};
const root = './';
const db = getValidDb(
'db.json',
{ injectors, root, reverse, dbMode, mockServer }
);
const middleware = getValidMiddlewares(middlewares, { root, mockServer });
const injectors = getValidInjectors(injectors, { root, mockServer });
const rewriters = getValidRewriters(rewriters, { root, mockServer });
const config = getValidConfig(config, { root, mockServer });
const store = getValidStore(store, { root });
const routeConfig = getValidRouteConfig(route, routeConfig);
const injectorConfig = getValidInjectorConfig(route, routeConfig);
const route = getValidRoute(route);
VS Code Extension
Thinker.mock-server
Author
Sivaraman - sendmsg2siva.siva@gmail.com
License
MIT