Package Exports
- @r35007/mock-server
- @r35007/mock-server/dist/server/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@r35007/mock-server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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
- Getting started
- Using JS Module
- Route Config
- Middleware Utils
- Injectors
- Store Data
- Route Rewriters
- Locals
- Config
- Home Page
- CLI Usage
- API
- VS Code Extension
- Author
- License
Getting started
Install Mock Server
npm install -g @r35007/mock-serverCreate a db.json file with some data.
{
"posts": [{ "id": 1, "title": "mock-server", "author": "r35007" }],
"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],
"profile": { "name": "r35007" }
}Start Mock Server
mock-server --watch ./db.jsonNow if you go to http://localhost:3000/posts/1, you'll get
{ "id": 1, "title": "mock-server", "author": "r35007" }Using JS Module
Install nodemon for watching changes
npm install -g nodemonCreate server.js File
const { MockServer } = require("@r35007/mock-server");
const config = { root: __dirname };
const mockServer = MockServer.Create(config);
mockServer.launchServer(
"./db.js",
"./injectors.json",
"./middleware.js",
"./rewriters.json",
"./store.json"
);or
const { MockServer } = require("@r35007/mock-server"); // use import if using ES6 module
// const MockServer = require("@r35007/mock-server").default; // For default import
// Provide config as a param. If not provided, It uses the default Config.
const port = 3000; // Set Port to 0 to pick a random available port. default: 3000
const host = "localhost"; // Set custom host. default: 'localhost'
const config = { root: __dirname, port, host }; // Config can also be given as a path to config file
const mockServer = MockServer.Create(config);
const app = mockServer.app; // Gives you the Express app
// Make sure to use this at first, before all the resources
const rewriter = mockServer.rewriter("./rewriters.json");
app.use(rewriter);
// Returns the default middlewares
// Provide options here. If not provided the options are picked from the default Config
const defaultsMiddlewares = mockServer.defaults();
app.use(defaultsMiddlewares);
// Custom Middleware
app.use((req, res, next) => {
if (isAuthorized(req)) {
next(); // continue to Mock Server router
} else {
res.sendStatus(401);
}
});
const isAuthorized = (req) => {
// add your authorization logic here
return true;
};
// Custom Routes
// This route will not be listed in Home Page.
app.get("/echo", (req, res) => {
res.jsonp(req.query);
});
// Loaded all the resources and returns the default router
const resources = mockServer.resources(
"./db.js",
"./injectors.json",
"./middleware.js",
"./store.json"
);
app.use(resources);
// Create the default Routes which helps to run the Mock Server Home Page.
const homePage = mockServer.homePage();
app.use(homePage);
app.use(mockServer.pageNotFound); // Middleware to return `Page Not Found` as response if the route doesn't match
app.use(mockServer.errorHandler); // Default Error Handler
// Provide port and host name as a param.
// Default port : 3000, Default host: 'localhost'
mockServer.startServer();
// mockServer.startServer(0, 'localhost'); // can also set port and host hereNow go to terminal and type the following command to start the Mock Server.
nodemon server.jsRoute Config
Create a db.json file. Pay attention to start every route with /. For Example:
db.json
{
"/route/1, /route/2": {
// /route/1 and /route/2 shares the same config and mock data
"_config": true, // Make sure to set this to true to use this object as a route configuration.
"id": "id-unique", // sets a base64 encoded route
"description": "", // Description about this Route.
"delay": 2000, // in milliseconds
"statusCode": 200, // in number between 100 to 600
"mockFirst": false, // If true, It sends the mock first else try to fetch first.
"middlewares": ["_IterateResponse"], // list of middleware names and methods to be called
"ignoreMiddlewareWrappers": false, // !Be cautious : If true it wont add any helper middleware wrappers. If true providing a middleware is must and no use of any other config like mock, fetch, predefined middlewares etc...
"fetch": "./myFile.json", // this path will be relative to `config.root`
"fetchCount": 5, // Set to -1 to fetch infinite times.
"mock": [{ "name": "foo" }, { "name": "bar" }],
"skipFetchError": false, // If True it skips any fetch error and send the mock data
"fetchData": {
"status": 200,
"isError": false,
"response": "" // contains the fetch response
},
"store": {}, // helps to store any values for later use
// System generated.
//Please don't set any values to the below attributes.
"_isFile": false, // sets to true if the given fetch url is a file type url
"_request": {}, // give the actual axios request object of the fetch call
"_extension": "" // If the given url is a file type url then it give the extension of the file.
},
"/routeName3": {
"name": "foo",
"age": "bar",
"description": "Note: Since _config attribute is not set to true this whole object will be sent as a response"
}
}Note :
fetchcan be eitherstringtype orAxiosRouteConfigtype. see Fetch Data From URL for more configuration.
Set Delay Response
delay helps you to set a custom delay to your routes.
Note : The delay yo set must be in milliseconds and of type number
{
"/customDelay": {
"_config": true,
"delay": 2000,
"description": "Note: give delay in milliseconds",
"mock": "This is response is received with a delay of 2000 milliseconds"
}
}Now if you go to http://localhost:3000/customDelay, you'll get the response in a delay of 2 seconds.
Set Custom StatusCode
statusCode helps you set a custom statusCode to your 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 you go to http://localhost:3000/customStatusCode, you'll get the response with a 500 statusCode
Fetch Data From URL
fetch helps you get data from url.
The url can either be a http server or a local file server.
Fetch File
Give a absolute or a relative path to fetch any file and get as a response.
Note: The given relative path will be relative to
config.root.
{
"/fetch/local/file": {
"_config": true,
"description": "The given fetch path will be relative to the root path given in config",
"fetch": "./data/users.json"
}
}http://localhost:3000/fetch/local/file.
Custom Fetch Options
You can also give a fetch as a axios request object with custom options.
{
"/fetch/posts/customOptions/: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}"
},
"fetchCount": 5
}
}http://localhost:3000/fetch/posts/customOptions/2.
Note: The 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
Use Fetch as Proxy
By directly giving a url it acts like a proxy. It sends all the options like prams, query params, headers, data etc.. from the mock route
{
"/fetch/comments/proxy": {
"_config": true,
"description": "When you directly give the url, all the request query params and posted body data will also to sent to the given url",
"fetch": "http://jsonplaceholder.typicode.com/comments",
"fetchCount": -1
},
"/posts/:id?": {
"_config": true,
"fetch": "http://jsonplaceholder.typicode.com/${req.url}" // will become http://jsonplaceholder.typicode.com/posts/1
}
}Note:
${req.url}will prepends the route to the url.
Now try the following url.
http://localhost:3000/posts/1.
http://localhost:3000/fetch/comments/proxy?postId=1.
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. You 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 you 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 you the mock data.
{
"/fetch/404/skipFetchError": {
"_config": true,
"description": "Bu default fetch returns the actual error if occur. If you set `skipFetchError` flag to true. the If any error occur in fetch call it will then skips the fetch error and return you the mock data",
"fetch": "http://localhost:3000/404",
"skipFetchError": true,
"mock": "This data is returned due to some error in fetch call. You can see the error in 'fetchError' attribute",
"fetchCount": -1
}
}http://localhost:3000/fetch/404/skipFetchError.
Add Middleware
You can add n number of middleware to a route which helps you to manipulate or log the data.
middleware.js
exports.DataWrapper = (req, res, next) => {
res.locals.data = {
status: "Success",
message: "Retrieved Successfully",
result: res.locals.data,
};
next();
};db.js
const db = {
"/fetch/users1/customMiddleware": {
_config: true,
fetch: "http://jsonplaceholder.typicode.com/users",
middlewares: ["DataWrapper"], // Picks the DataWrapper middleware from middleware.js
},
"/fetch/users2/customMiddleware": {
_config: true,
fetch: "http://jsonplaceholder.typicode.com/users",
middlewares: [
(req, res, next) => {
res.locals.data = {
status: "Success",
message: "Retrieved Successfully",
result: res.locals.data,
};
next();
},
],
},
};http://localhost:3000/fetch/users/customMiddleware.
Ignore Middleware Wrappers
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 ignoreMiddlewareWrappers to true.
For Example:
db.js
const db = {
"/static/app1": express.static("./path/to/build/folder/app1") // will ignore helper middleware wrappers
"/static/app2": { _config: true, middlewares: express.static("./path/to/build/folder/app2"), ignoreMiddlewareWrappers: true }
"/fetch/users": { _config: true, fetch: "http://jsonplaceholder.typicode.com/users", ignoreMiddlewareWrappers: true }
}Note:
/fetch/userswont work since it wont be wrapped by helper middlewares and so no other route config would work except the given middleware if provided.
Middleware Utils
Use the predefined middleware to speedup your development and for ease of access.
IterateResponse
setting middleware to _IterateResponse helps to send you a iterate the response one after the other in the mock array for each url hit.
example:
{
"/middleware/utils/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/utils/example/_IterateResponse. For each hit you will get the next object in an array from the photos data.
IterateRoutes
setting middleware to _IterateRoutes helps to send you a iterate the route one after the other in the mock array for each url hit.
example:
{
"/middleware/utils/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/utils/example/_IterateRoutes. For each hit the route is passed to next matching url provided in the mock list.
AdvancedSearch
_AdvancedSearch middleware helps to filter and do the advanced search from data.Following are the operations performed by this method.
Filter
Use . to access deep properties
GET /posts?title=mock-server&author=sivaraman
GET /posts?id=1&id=2
GET /comments?author.name=sivaramanPaginate
Use _page and optionally _limit to paginate returned data.
In the Link header you'll get first, prev, next and last links.
GET /posts?_page=7
GET /posts?_page=7&_limit=2010 items are returned by default
Sort
Add _sort and _order (ascending order by default)
GET /posts?_sort=views&_order=asc
GET /posts/1/comments?_sort=votes&_order=ascFor multiple fields, use the following format:
GET /posts?_sort=user,views&_order=desc,ascSlice
Add _start and _end or _limit (an X-Total-Count header is included in the response)
GET /posts?_start=20&_end=30
GET /posts/1/comments?_start=20&_end=30
GET /posts/1/comments?_start=20&_limit=10Works exactly as Array.slice (i.e. _start is inclusive and _end exclusive)
Operators
Add _gte or _lte for getting a range
GET /posts?views_gte=10&views_lte=20Add _ne to exclude a value
GET /posts?id_ne=1Add _like to filter (RegExp supported)
GET /posts?title_like=serverFull text search
Add q or _text
GET /posts?q=internet&_text=successCrudOperations
_CrudOperations middleware handles all the crud operations of the given data.
By default it also handles the _AdvancedSearch operations.
Note: the mock must of type Array of objects and must contain a unique value of attribute
id. Thisidattribute can also be changes usingconfig.id.
For example: config.json
{
"id": "_id"
}FetchTillData
_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
_SetFetchDataToMock sets every fetchData to Mock.
This overrides the existing mock with the fetchData.
SetStoreDataToMock
_SetStoreDataToMock sets every store data to Mock data.
This overrides the existing mock with the store.
MockOnly
_MockOnly sends you only Mock data even if there is any fetchData or store data.
FetchOnly
_FetchOnly sends you only Fetch data even if there is any _mock or store data.
ReadOnly
_ReadOnly forbidden every Http method calls except GET call.
Fetch
_Fetch Helps to fetch from file path or from the url.
Note:
locals.routeConfig.fetchmust contain a valid file path or http url to fetch the data.
For Example:
db.json
{
"/user": "./mock/user.json" // directly setting the path instead of setting it as a fetch config
}injectors.js
module.exports = [
{
routes: ["/user"],
middlewares: [
(req, res) => {
const routeConfig = res.locals.routeConfig;
routeConfig.fetch = routeConfig.mock;
},
"_Fetch", // helps to fetch the file from routeConfig.fetch path
],
},
];FetchFile
_FetchFile Helps to fetch from file path.
Note:
locals.routeConfig.fetchmust contain a valid file path to fetch the data.
FetchUrl
_FetchUrl Helps to fetch from http url.
Note:
locals.routeConfig.fetchmust contain a valid http url to fetch the data.
Injectors
Injectors helps to inject a Route Configs explicitly.
This also helps to provide a common route configs.
Injector uses path-to-regexp pattern recognition to set config for multiple routes.
Inject Route Configs
Here we are explicitly injecting delay, middlewares, statusCode to the /posts route.
You can any route configs to a specific or to a group of routes using Injectors.
- Injectors use
path-to-regexppackage for route pattern recognition. - Click path-to-regexp for more details.
example : injectors.json
[
{
"routes": ["/injectors/:id"],
"description": "This description is injected using the injectors by matching the pattern '/injectors/:id'."
}
]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 you want to add the existing middlewares.
Common Route Configs
Using wildcards you 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 you give /(.*) at the end of the injectors.json object to set route configs to all the routes.
Store Data
Store used to store any values which can be used later for any purpose like response manipulation or logging etc..
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 _CrudOperations, _IterateRoutes, _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.
Route Rewriters
Create a rewriters.json file. Pay attention to start every route with /.
- Rewriters use
express-urlrewritepackage to rewrite the urls. - Click here for more information about url rewrite.
{
"/api/*": "/$1",
"/:resource/:id/show": "/:resource/:id",
"/posts/:category": "/posts?category=:category",
"/articles?id=:id": "/posts/:id"
}server.js
const mockServer = MockServer.Create();
mockServer.launchServer(
"./db.json",
"./injectors.json",
"./middleware.js",
"./rewriters.json"
);Now you can access resources using additional routes.
/api/posts # → /posts
/api/posts/1 # → /posts/1
/posts/1/show # → /posts/1
/posts/javascript # → /posts?category=javascript
/articles?id=1 # → /posts/1Locals
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: {
_config?: boolean;
id?: string;
description?: string;
mock?: any;
mockFirst?: boolean;
fetch?: string | AxiosRequestConfig;
fetchData?: {
status?: number;
message?: string;
isError?: boolean;
headers?: any;
response?: any;
stack?: any;
};
store?: object;
statusCode?: number;
delay?: number;
fetchCount?: number;
skipFetchError?: boolean;
middlewares?: Array<express.RequestHandler>;
// System generated attributes. Please avoid modifying these attribute values
_isFile?: boolean;
_request?: AxiosRequestConfig;
_extension?: string;
};
data: any; // response will be sent using this attribute value.
config: Config; // gives you the current mock server configuration.
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) {
// If fetchData has no error then stop fetching anymore
routeConfig.fetchCount = 0; // setting fetchCount to zero stops fetching
} else if (
routeConfig.fetchCount !== undefined &&
routeConfig.fetchCount == 0
) {
// If fetchData has any error then keep on fetching
routeConfig.fetchCount = -1; // setting fetchCount to -1 does an infinite fetch
}
next();
};The above middleware helps to fetch the data from url until it gets a valid success response.
Config
you can provide your own config by passing the config object in the MockServer constructor. For Example :
server.js :
// These are default config. You can provide your custom config as well.
const config = {
port: 3000, // Set Port to 0 to pick a random available port.
host: "localhost", // Set custom host
root: process.cwd(), // Root path of the server. All paths refereed in db data will be relative to this path
base: "", // Mount db on a base url
id: "id", // Set db id attribute.
dbMode: "mock", // Give one of 'multi', 'fetch', 'mock'
staticDir: "", // Path to host a static files
reverse: false, // Generate routes in reverse order
logger: true, // Enable api logger
noCors: false, // Disable CORS
noGzip: false, // Disable data compression
readOnly: false, // Allow only GET calls
bodyParser: true, // Enable body-parser
cookieParser: true, // Enable cookie-parser
};
new MockServer(config).launchServer("./db.json");Db Mode
Db mode defines on what config does the direct route value to be assigned. For Example :
dbModeismulti- Only directstringvalue will be assigned tofetchattribute. All other values will be assigned tomockattribute
const db = {
route1: "My Response", // String
route2: { data: "My Response" }, // Object without _config: true
route3: [], // Array
route4: "path/to/file", // Some path to fetch a file
route5: { _config: true, mock: "My Response" } // Config object.
}
// Transforms as follow
{
"/route1": { _config: true, fetch: "My Response" },
"/route2": { _config: true, mock: { data: "My Response" } },
"/route3": { _config: true, mock: [] }, // Array
"/route4": { _config: true, fetch: "path/to/file" },
"/route5": { _config: true, mock: "My Response" } // Config object wont be changed
}dbModeismock- All direct values will be assigned tomockattribute
const db = {
route1: "My Response", // String
route2: { data: "My Response" }, // Object without _config: true
route3: [], // Array
route4: "path/to/file", // Some path to fetch a file
route5: { _config: true, fetch: "path/to/file" }, // Config object.
}
// Transforms as follow
{
"/route1": { _config: true, mock: "My Response" },
"/route2": { _config: true, mock: { data: "My Response" } },
"/route3": { _config: true, mock: [] },
"/route4": { _config: true, mock: "path/to/file" },
"/route5": { _config: true, fetch: "path/to/file" } // Config object wont be changed
}dbModeisfetch- All direct values will be assigned tomockattribute
const db = {
route1: "My Response", // String
route2: { data: "My Response" }, // Object without _config: true
route3: [], // Array
route4: "path/to/file", // Some path to fetch a file
route5: { _config: true, mock: "My Response" }, // Config object.
}
// Transforms as follow
{
"/route1": { _config: true, fetch: "My Response" },
"/route2": { _config: true, fetch: { data: "My Response" } },
"/route3": { _config: true, fetch: [] },
"/route4": { _config: true, fetch: "path/to/file" },
"/route5": { _config: true, mock: "My Response" } // Config object wont be changed
}Home Page
Home Page- http://localhost:3000Db- http://localhost:3000/_dbRewriters- http://localhost:3000/_rewritersStore- http://localhost:3000/_storeReset Db- http://localhost:3000/_reset/db
CLI Usage
$ mock-server --help
Options:
-c, --config Path to config file [string]
-P, --port Set port [default: 3000]
-H, --host Set host [default: "localhost"]
-m, --middleware Paths to middleware file [string]
-i, --injectors Path to Injectors file [string]
-s, --store Path to Store file [string]
-r, --rewriters Path to Rewriter file [string]
--staticDir, --sd Set static files directory [string]
-b, --base Set base route path [string]
--readOnly, --ro Allow only GET requests [boolean]
--noCors, --nc Disable Cross-Origin Resource Sharing [boolean]
--noGzip, --ng Disable GZIP Content-Encoding [boolean]
-l, --logger Enable logger [boolean] [default: true]
--sample, --ex Create Sample [boolean] [default: false]
-w, --watch Watch file(s) [boolean] [default: false]
-S, --snapshots Set snapshots directory [default: "."]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Examples:
index.js db.json
index.js --watch db.json
index.js --sample --watch
index.js http://jsonplaceholder.typicode.com/db
https://r35007.github.io/Mock-Server/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
stops the server and clears the instance of the mockServer. returns promise
const { MockServer } = require("@r35007/mock-server");
const mockServer = MockServer.Create();
await MockServer.Destroy();
// or
// pass a mock server instance to destroy
await MockServer.Destroy(mockServer);launchServer
It validates all the params in the MockServer, loads the resources and starts the server.
mockServer.launchServer(
"./db.json",
"./injectors.json",
"./middleware.js",
"./rewriters.json",
"./store.json"
);Params
| Name | Type | Required | Description |
|---|---|---|---|
| db | string / object | No | This object generates the local rest api. |
| injectors | string / object | No | Helps to inject a route configs for the existing routes |
| middleware | string / object | No | Here you initialize the needed custom middleware |
| rewriters | string / object | No | Helps to set route rewriters |
| store | string / object | No | Helps to store values and share between routes |
rewriter
Sets the route rewrites and return the router of the rewriters;
const rewriters = mockServer.rewriter("./rewriters.json");
app.use(rewriters);Params
| Name | Type | Required | Description |
|---|---|---|---|
| rewriters | string / object | No | Give the Rewrites |
defaults
returns the list of default middlewares. Also helps to host a static directory.
const defaults = mockServer.defaults({ staticDir: "./public", readOnly: true });
app.use(defaults);- options
staticDirpath to static filesloggerenable logger middleware (default: true)bodyParserenable body-parser middleware (default: true)noGzipdisable Compression (default: false)noCorsdisable CORS (default: false)readOnlyaccept only GET requests (default: false)cookieParserenable cookie parser (default: true)
resources
Sets the routes and returns the resources router.
const resources = mockServer.resources(
"./db.json",
"./injectors.json",
"./middleware.js",
"./store.json"
);
app.use(resources);Params
| Name | Type | Required | Description |
|---|---|---|---|
| db | string / object | No | This object generates the local rest api. |
| injectors | string / object | No | Helps to inject a route configs for the existing routes |
| middleware | string / object | No | Here you initialize the needed custom middleware |
| store | string / object | No | Helps to store values and share between routes |
homePage
Returns Mock Server Home Page router.
const homePage = mockServer.homePage();
app.use(homePage);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(); // If param is not present, it resets all the routes.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
// Please avoid directly modify or setting values to these variable.
// Use Setter method to modify or set any values.
// server, port, address, listeningTo will be undefined when server is stopped
const port: number | undefined; // gives current running port.
const server: Server | undefined; // gives current running server.
const address: string | undefined; // gives host ip address.
const listeningTo: string | undefined; // gives -> http://${host}:${port}/${base} -> http://localhost:3000/api
const app = mockServer.app;
const data = mockServer.data;
// const { db, injectors, middlewares, rewriters, config, store } = 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;Setters
mockServer.setData(db, injectors, middlewares, rewriters, store, config);
//or
// Please follow the same following order of setting the data
// If merge param is true. it adds the data with the existing data.
mockServer.setConfig(config, merge);
mockServer.setMiddlewares(middlewares, merge);
mockServer.setInjectors(injectors, merge);
mockServer.setRewriters(rewriters, merge);
mockServer.setStore(store, merge);
mockServer.setDb(Db, merge);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/server/utils/validators");
const options = {
reverse: true, // If true the db will be generated in reverse order
dbMode: "fetch", // The direct route value will be set to fetch
};
const rootPath = "./";
const db = getValidDb(
"db.json", // db or HAR
[], // injectors
rootPath, // Root path to fetch the db.json file
options
); // returns valid Db combined with the given injectors. Also helps to extract a db from HAR file. internally use getValidRouteConfig
const middleware = getValidMiddlewares(middlewares, rootPath); // returns a valid middleware along with the default middlewares
const injectors = getValidInjectors(injectors, rootPath); // returns a valid injectors. internally use getValidInjectorConfig
const rewriters = getValidRewriters(rewriters, rootPath); // returns a valid rewriters
const config = getValidConfig(config, rootPath); // returns a valid config combined with the default configs
const store = getValidStore(store, rootPath); // returns a valid store
const routeConfig = getValidRouteConfig(route, routeConfig); // returns a valid routeconfig used by getValidDb
const injectorConfig = getValidInjectorConfig(route, routeConfig); // returns a valid injectorsConfig used by getValidInjectors
const route = getValidRoute(route); // splits route by comma and adds a slash ('/') prefix to the routesVS Code Extension

Author
Sivaraman - sendmsg2siva.siva@gmail.com
License
MIT