http-fake-backend

Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.

GitHub version Build Status Code Climate Coverage Status Dependency Status devDependency Status Known Vulnerabilities Unicorn

http-fake-backend

Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.

Comes as a Node.js server. Useful for mocking, testing and developing independent of the »real« backend.

Example

Let’s say you need an endpoint like http://localhost:8081/api/example which should return:

{
  "response": "Yeah"
}

It’s a matter of seconds to create this endpoint with help of this little hapi server.

It might take a few seconds longer as setting up the well-made JSON Server but it’s way more flexible.

Requirements

  • Node.js (v4.0.0 or greater)

Install

git clone https://github.com/micromata/http-fake-backend.git
npm install

Hint: Use yarn install instead of npm install if you have installed Yarn … and yup, we have a lock file :sparkling_heart:

Or with help of Yeoman

npm install -g yo
npm install -g generator-http-fake-backend

This comes in handy, because the Yeoman generator has a sub-generator to setup endpoints of your fake backend very convenient. See https://github.com/micromata/generator-http-fake-backend.

Default Address

The server runs at http://localhost:8081/ providing a page with links to all existing API endpoints.

Start the server

There are the following two options.

During development

npm run start:dev

This way the server uses nodemon to restart itself on changes. This way you dont have to restart the server in case you changed an endpoint.

Later (eg. for tests in CI)

npm start

Just fires up the server via node.
This one comes is without any magic (eg. foreverjs)

Configuring endpoints

Each endpoint needs a configuration file in /server/api/ to define routes, http method and the response.

Example configurations

Simple Example

/server/api/simpleExample.js:

module.exports = SetupEndpoint({
    name: 'simpleExample',
    urls: [{
        requests: [
            { response: '/json-templates/simpleExample.json' }
        ]
    }]
});

Advanced Example

/server/api/anotherExample.js:

module.exports = SetupEndpoint({
    name: 'anotherExample',
    urls: [{
        params: '/read',
        requests: [{
            method: 'GET',
            response: '/json-templates/anotherExample.json'
        }]
    }, {
        params: '/update/{id}',
        requests: [{
            method: ['PUT', 'PATCH'],
            response: {
                success: true
            }
        }, {
            method: 'DELETE',
            response: {
                deleted: true
            }
        }]
    }]
});

Faking HTTP errors and status code

/server/api/fakingStatusCodes.js:

module.exports = SetupEndpoint({
    name: 'statusCodes',
    urls: [
        {
            params: '/boomError',
            requests: [{
                // Returns a 402 status code + error message provided by boom:
                // {
                //   "error" : "Payment Required",
                //   "statusCode" : 402
                // }
                statusCode: 402
            }]
        },
        {
            params: '/customError',
            requests: [{
                // Returns a HTTP status code 406 and a self defined response:
                response: { error: true },
                statusCode: 406
            }]
        },
        {
            params: '/regularResponse',
            requests: [{
                // Returns a 401 error provided by boom
                // as defined on endpoint level
                response: '/json-templates/anotherExample.json'
            }]
        }
    ],
    statusCode: 401
});

The configuration object in Detail:

  • name
    • Is used to set the endpoint.
  • urls
    • You need to add at least one url object.
  • urls.params
    • Optional
    • URL path parameters with fixed and/or variable path segments.
    • Example:
    • params: '/update/{id}'
    • See hapi docs. For example regarding optional path parameters.
  • urls.requests
    • You need to add at least one request object.
    • Multiple request objects are needed in case you like to serve different responses via different HTTP methods with the same URL.
  • urls.requests.method
    • optional. Uses GET when not defined.
    • string, or array of strings.
    • is used to define the http method(s) to which the endpoint will listen.
  • urls.requests.response
    • Could be a string pointing to a JSON template:
    • response: '/json-templates/articles.json'
    • Or just a JavaScript object:
    • response: { success: true }
  • urls.requests.statusCode
    • Optional
    • A status code (number)
    • Will return:
    • a status code with a self defined response if you provide a response property
    • a status code with a predefined error object provided by boom if you dont provide a response property for that request.
  • statusCode
    • Optional
    • Every subroute of this endpoint will return a HTTP error with the given status code provided by boom.

Configuration

The main config is handled via a file named .env with the following content:

# NODE_ENV
# Could be either `development` or `production`
NODE_ENV=development

# Port of the Server
SERVER_PORT=8081

# Port for running the tests
TEST_PORT=9090

# URL Prefix for the endpoints
# eg. http://localhost:8081/api/foo
API_PREFIX=/api

Related

  • Yeoman Generator – Easily generate your fake backend and use sub-generators to setup endpoints like :zap:

License

Please be aware of the licenses of the components we use in this project. Everything else that has been developed by the contributions to this project is under MIT License.

Related Repositories

interfake

interfake

:computer: Fake APIs for prototypes & automated tests. ...

dyson

dyson

Node server for dynamic, fake JSON. ...

nginx-upsync-module

nginx-upsync-module

nginx-upsync-module, sync upstreams from consul or etcd and so on, dynamiclly mo ...

backend-best-practices

backend-best-practices

An evolving description of general best practices for backend development. ...

pusher-fake

pusher-fake

A fake Pusher server for development and testing. ...