Usage
- Build simple fake server with routing, params, static content
- GET, POST, PUT, DELETE, supported methods, status, bodies etc

Install
npm install -SD json-fake-server || npm i -g json-fake-server
Example
base usage example
const fakeServer = require('json-fake-server')
const model = {
port: 9090,
api: [{
method: "GET",
path: "/",
response: "Hello world"
}]
}
const server = fakeServer(model)
setTimeout(() => {
server.stop()
}, 25000)
mocha test example
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const {expect} = require('chai')
const model = {
"port": 8888,
"api": [
{
"method": "GET",
"path": "/user",
"response": {
"user_name": "test user"
}
},
{
"method": "POST",
"path": "/user",
"response": {"created": true}
}
]
}
describe('Example', () => {
let server = null
before(() => {
server = fakeServer(model)
})
after(() => {
server.stop()
})
it('test post user', asyn () => {
const responseBody = await fetch('http://localhost:8888/user', {method: 'POST'}).then((res) => res.json())
expect(responseBody.created).to.eql(true)
})
it('test get user', async () => {
const responseBody = await fetch('http://localhost:8888/user').then((res) => res.json())
expect(responseBody.user_name).to.eql('test user')
})
})
Example from command line
./test.json
{
"port": 8081,
"host": "0.0.0.0",
"api": [
{
"method": "GET",
"path": "/example",
"response": {
"example": "example GET"
}
}
]
}
json-fake-server -m ./test.json
More examples
Model Structure
Endpoint Model Object
const APIModelObject = {
"method": "GET",
"path": "/example/:param1/:param2",
"status": 200,
"authorization":{
"unauthorized": {
"foo": "bar"
},
"status": 401,
"token":"testToken"
},
"params_response": {
"response": {
"allParamsAreEqual": {
"param1": "success",
"param2": "success"
}
},
"param1": {
"value": "testFirst",
"response": {
"testId": "testFirst"
},
"param1": {
"value": "testSecond",
"response": {
"testId": "testSecond"
}
}
},
"request_body_equal": {
"status": 404,
"not_equal_response": {
"success": false
},
"expected_body": {
"username": "test_",
"password": "test_pass"
}
},
"response": {
"example": "example GET"
},
}
HTTP methods
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const model =
{
"port": 8081,
"api": [
{
"method": "GET",
"path": "/example",
"response": {
"example": "example GET"
}
},
{
"method": "POST",
"path": "/example",
"response": {
"example": "example POST"
}
},
{
"method": "DELETE",
"path": "/example",
"response": {
"example": "example DELETE"
}
},
{
"method": "PUT",
"path": "/example",
"response": {
"example": "example PUT"
}
}
]
}
const server = fakeServer(model)
async function callToServer() {
const postData = await fetch('http://localhost:8888/example', {method: 'POST'}).then((res) => res.json())
const getData = await fetch('http://localhost:8888/example', {method: 'GET'}).then((res) => res.json())
const putData = await fetch('http://localhost:8888/example', {method: 'PUT'}).then((res) => res.json())
const deleteData = await fetch('http://localhost:8888/example', {method: 'DELETE'}).then((res) => res.json())
}
Authorization
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const authorizationInApiObj = {
"unauthorized": {
"foo": "bar"
},
"status": 401,
"token":"testToken"
}
const model = {
"port": 8081,
"authorization": {"type": "headers"},
"api": [
{
"method": "GET",
"path": "/example",
"response": {"example": "example GET"},
"authorization": authorizationInApiObj
}
]
}
const server = fakeServer(model)
async function callToServerHeaderAuthorization() {
const withoutTokenData = await fetch('http://localhost:8888/example', {method: 'GET'}).then((res) => res.json())
const withTokenData = await fetch('http://localhost:8888/example', {
headers: {Authorization: 'Bearer testToken'},
method: 'GET'}).then((res) => res.json())
}
callToServer()
Params
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const model = {
"port": "8081",
"api": [{
"method": "GET",
"path": "/user/:user/id/:id",
"params_response": {
"id": {
"value": "testId",
"response": {
"testId": "testId"
}
},
"user": {
"value": "testUser",
"response": {
"user": "testId"
}
},
"response": {
"full_params_equal": {
"username": "test user1",
"password": "test password"
}
}
},
"response": {
"example": "example GET"
}
}]
}
async function callToServer() {
const defaultGetData = await fetch('http://localhost:8081/user/unknown/id/unknown', {method: 'GET'}).then((res) => res.text())
console.log(defaultGetData)
const fullPramsEqual = await fetch('http://localhost:8081/user/testUser/id/testId', {method: 'GET'}).then((res) => res.text())
console.log(fullPramsEqual)
const userEqualParamEqual = await fetch('http://localhost:8081/user/testUser/id/unknown', {method: 'GET'}).then((res) => res.text())
console.log(userEqualParamEqual)
const idEqualParamEqual = await fetch('http://localhost:8081/user/unknown/id/testId', {method: 'GET'}).then((res) => res.text())
console.log(idEqualParamEqual)
}
Default response
Full params equal response
Partial equal param user
Partial equal param id
Queries
const fakeServer = require('../')
const fetch = require('node-fetch')
const model_obj = {
"port": "8081",
"api": [{
"method": "GET",
"path": "/test",
"response": {
"testOne": 1,
"testTwo": 2,
"testThree": 3,
"testFour": 4,
}
}]
}
const model_array = {
"port": "8082",
"api": [{
"method": "GET",
"path": "/test",
"response": [
{
"testOne": 1,
"testTwo": 2,
"testThree": 3,
"testFour": 4,
},
{
"testOne": 1,
"testTwo": 2,
"testThree": 3,
"testFour": 4,
},
{
"testOne": 1,
"testTwo": 2,
"testThree": 3,
"testFour": 4,
}
]
}]
}
async function callToServer() {
server_obj = fakeServer(model_obj)
server_array = fakeServer(model_array)
const query_resp_obj = await fetch('http://localhost:8081/test?testOne=1&testTwo=2', {method: 'GET'}).then((res) => res.text())
console.log(query_resp_obj)
const query_resp_array = await fetch('http://localhost:8082/test?testOne=1&testTwo=2', {method: 'GET'}).then((res) => res.text())
console.log(query_resp_array)
}
HTML
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const path = require('path')
const indexHtml = path.resolve(__dirname, './index.html')
const model = {
"port": "8081",
"api": [{
"method": "GET",
"path": "/",
"response": indexHtml
}]
}
async function callToServer() {
const indexHtmlText = await fetch('http://localhost:8081/', {method: 'GET'}).then((res) => res.text())
console.log(indexHtmlText)
}
Request body assertion
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const model_obj = {
"port": "8081",
"debug": true,
"api": [{
"method": "POST",
"path": "/test",
"request_body_equal": {
"status": 404,
"not_equal_response": {
"success": false
},
"expected_body": {
"username": "test_",
"password": "test_pass"
}
},
"response": {
"success": true
}
}]
}
const serser = fakeServer(model_obj)
async function callToServer() {
const body_equal_success = await fetch('http://localhost:8081/test', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({"username": "test_", "password": "test_pass"})
}).then((res) => res.text())
console.log(body_equal_success)
const body_not_equal = await fetch('http://localhost:8081/test', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
"username": "test_1",
"password": "test_pass"
})
}).then((res) => res.text())
console.log(body_not_equal)
serser.stop()
}
Several server nodes in one environment
const fakeServer = require('json-fake-server')
const fetch = require('node-fetch')
const model_entry_point = {
"port": 8081,
"api": [
{
"method": "GET",
"path": "/user",
"response_from_url": {
"status": 201,
"method": "GET",
"url": "http://localhost:8888/userData",
"merge_with": {
"part_from_entrypoint": "entry point"
}
}
}
]
}
const model_user = {
"port": 8888,
"api": [
{
"method": "GET",
"path": "/userData",
"response": {
"part_from_user_service": {
"user_profile": {
"username": "some username",
"postal_code": 3212654
}
}
}
}
]
}
const entry = fakeServer(model_entry_point)
const userSerice = fakeServer(model_user)
async function callToServer() {
const getData = await fetch('http://localhost:8081/user',
{method: 'GET'}).then((res) => res.json())
console.log(getData)
entry.stop()
userSerice.stop()
}