@edwardmx/mockserver v0.0.3
Mock server
To build full fake REST API with json-server for Front-end developer who need a quick Back-end for prototyping and mocking service responses.
Table of Contents
Instalation
You need to first install it via npm:
$ npm install @edwardmx/mockserver --save-dev
Next, create javascript file and include library:
const server = require('@edwardmx/mockserver');
server();
Finally, create mockserver.config.json
file in root folder.
{
"description": "RestFul Mock API",
"port": 8000,
"prefix": "/public.api.mx",
"folder": {
"root": "/api/",
"public": "public/",
"protected": "protected/",
"users": "protected/users.json"
},
"security": {
"prefix": "/private.api.mx",
"enabled": true,
"endpoint": "/login",
"schemma": "Bearer",
"jwt": {
"secret": "hibhturIei47eoaWbACns7nl17o8k0YP",
"claims": {
"issuer": "sso.localhost.mx",
"expiresIn": "10m",
"audience": "aud.localhost.mx",
"subject": "mock:hn7LT49LG93oioVq"
},
"data": [
"name",
"lastName",
"email",
"phone",
"website",
"company"
]
},
"lastLogin": {
"value": 2,
"type": "days"
}
},
"writeFile": false
}
Start the server with the following command:
node mockserver.js
Database
The database files saved into folder api
in root folder. Save some data in a JSON file. Eg.
- api
- protected
- users.json
- actors.json
- private
- movies.json
- protected
users.json
{
"users": [
{
"id": 1,
"name": "Susie",
"lastName": "Martínez",
"email": "susie.martinez@gmail.com",
"password": "Abcd1234$",
"phone": "1122334455",
"website": "susiemartinez.mx"
},
{
"id": 2,
"name": "Tonya",
"lastName": "Keller",
"password": "Abcd1234$",
"email": "tonya.keller@gmail.com",
"phone": "9922776633",
"website": "tonyakeller.io"
},
{
"id": 3,
"name": "Greg",
"lastName": "Boyd",
"password": "Abcd1234$",
"email": "greg.boyd@gmail.com",
"phone": "6644993300",
"website": "gregboyd.net"
}
]
}
actors.json
{
"actors": [
{
"name": "Catherine Missal",
"rating": 4875,
"image_path": "/g3fsRgEoMxaqPayIMtGDWERqJ6A.jpg",
"alternative_name": null,
"objectID": "551486300"
},
{
"name": "Monica Bellucci",
"rating": 3956,
"image_path": "/z3sLuRKP7hQVrvSTsqdLjGSldwG.jpg",
"alternative_name": "Monica Anna Maria Bellucci",
"objectID": "551486310"
},
{
"name": "Tom Cruise",
"rating": 2237,
"image_path": "/3oWEuo0e8Nx8JvkqYCDec2iMY6K.jpg",
"alternative_name": null,
"objectID": "551486340"
},
{
"name": "Scarlett Johansson",
"rating": 2128,
"image_path": "/f3c1rwcOoeU0v6Ak5loUvMyifR0.jpg",
"alternative_name": "Scarlett Johanssen",
"objectID": "551486350"
}
]
}
Configuration
The mockserver.config.json
file defines all configuration for create mock server.
Item | Description |
---|---|
description | Description for mock server |
port | Port number for running server |
prefix | Define prefix for all endpoints public |
folder.root | Path for root folder |
folder.users | File name contains users data |
folder.endpoints | File name contains proxy endpoints (middleware) |
folder.protected | Folder that contains all the JSON files that make up the protected endpoints |
folder.public | Folder that contains all the JSON files that make up the public endpoints |
security.prefix | Define prefix for all endpoints protected |
security.enabled | true for indicates load security configuration |
security.endpoint | Endpoint name for login service |
security.schemma | Schemma for authorization header (Basic, Bearer) |
security.jwt.secret | String security for JWT |
security.jwt.claims.issuer | String or array of strings of valid values for the iss field |
security.jwt.claims.expiresIn | Expires time for JWT |
security.jwt.claims.audience | The audience can be checked against a string, a regular expression or a list of strings and/or regular expressions |
security.jwt.claims.subject | If you want to check subject (sub), provide a value here |
security.jwt.data | Array list with field data user |
lastLogin.value | Number for defined last login |
lastLogin.type | String define last login type (seconds, minutes, hours days, etc) |
writeFile | true for indicates write file with all JSON files |
Endpoints
Create the endpoints.json
file inside the api folder. This file will contain all routes that will be rewritten with a new route. Each path must have its own JSON file with the response structure, all routes will be treated with the GET method.
The structure for the routes is:
{
"/public.api.mx/movies/genres": {
"to": "/public.api.mx/genres"
},
"/private.api.mx/movies/directors": {
"to": "/private.api.mx/directors"
}
}