Renato Ivancic - Software Engineer

Background image

Create mock REST API Server

Backend

Mock REST API with json-server and support of faker and lodash npm modules.

In case you need lightweight-lightningfast deployment of fake REST API for prototyping, get Docker image that has npm json-server module installed. Just save static data in JSON file. Image also includes faker and lodash modules so instead of static JSON initialization file you can script a ton of data.

Docker image: json-server-faker

Run command:

docker run -d --name mock_server -p 3000:80 -v /<your local dir>/file.js:/data/file.js rivancic/json-server-faker

Example JavaScript initialization file file.js:

module.exports = function() {
    var faker = require("faker");
    var _ = require("lodash");
    return {
        categories: _.times(6, function(n) {
            return {
                image: faker.random.image(),
                title: faker.lorem.sentence(),
                id: faker.random.uuid()
            }
        }),
        products: _.times(100, function(n) {
            return {
                id: faker.random.uuid(),
                images: [faker.random.image(), faker.random.image(), faker.random.image(), faker.random.image(), faker.random.image()],
                category-id: faker.random.uuid(),
                price: faker.commerce.price(),
                name: faker.commerce.productName(),
                release-date: faker.date.future(),
                format: faker.commerce.productMaterial(),
                label: faker.lorem.sentence(),
                recommended-age: faker.random.number(),
                title: faker.lorem.sentences()
            }
        }),
        users: _.times(100, function(n) {
            return {
                password: faker.internet.password(),
                email: faker.internet.email(),
                user-token: faker.random.uuid(),
                first-name: faker.name.firstName(),
                last-name: faker.name.lastName(),
                address: faker.address.streetAddress(),
                city: faker.address.city(),
                zip: faker.address.zipCode()
            }
        })
    }
}

json-server

json-server logo

Are you building some client application and the backend doesn’t exists yet, or you need some quick dirty prototype to play with. Json-server is really simple mock REST API server that serves JSON responses. POST, PUT, PATCH or DELETE requests are making changes automatically and are safely saved to db.json using lowdb. Based on the initialization file the routes are created for you. Even filtering, searching, slicing and sorting is supported. It has the ability to run server really fast with no real programming. On the other hand it does not have some framework for custom handling of requests like MockServer for example. Data source can be JSON or JavaScript file.

Faker

faker-server logo

To avoid monotonous creation of humongus JSON file you can help yourself with Faker module (with quite impressive logo). So the different data formats and views on client application can be tested. Among other you can create fake addresses, names, dates, finance data, images, lorem ipsum sentences… It also supports localization.

[Lodash] (https://lodash.com/) is used as syntactic sugar.

Deployment

Easy way: install nodejs and npm on your computer. Then npm install json-server and optionally also faker and lodash. The json-server can be started with below command:

$ json-server --watch db.json

Or even easier; deploy the docker image as described on beginning of this post.

Next step

Didn’t have time yet but I found an interesting project that provides creation of JSON objects with JSONShema that already contains faker functionality. I think it is sound evolution of above system. JSON Schema Faker

Sources

docker package json-server-faker

json-server

[faker] (https://github.com/Marak/faker.js)

[lodash] (https://lodash.com/)

clue docker json-server

#json-server #docker #faker #rest #test