All Projects → micromata → Http Fake Backend

micromata / Http Fake Backend

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Http Fake Backend

Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-80.63%)
Mutual labels:  api, rest-api, rest, restful-api, restful, json, fake, data, mock, mocking, backend, http-server
Apidoc
RESTful API 文档生成工具,支持 Go、Java、Swift、JavaScript、Rust、PHP、Python、Typescript、Kotlin 和 Ruby 等大部分语言。
Stars: ✭ 785 (+210.28%)
Mutual labels:  api, rest-api, rest, restful-api, restful
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+211.07%)
Mutual labels:  api, rest-api, rest, restful-api, restful
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1262.85%)
Mutual labels:  api, rest-api, rest, mock, mocking
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (-42.29%)
Mutual labels:  api, rest-api, rest, restful-api, restful
Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (-92.89%)
Mutual labels:  rest-api, rest, restful-api, restful, http-server
Api Strategy
Equinor API Strategy
Stars: ✭ 56 (-77.87%)
Mutual labels:  api, rest-api, rest, restful-api, restful
Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (-28.46%)
Mutual labels:  api, rest-api, rest, restful-api, restful
Json Serverless
Transform a JSON file into a serverless REST API in AWS cloud
Stars: ✭ 108 (-57.31%)
Mutual labels:  api, rest-api, rest, json
Watsonwebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
Stars: ✭ 125 (-50.59%)
Mutual labels:  rest-api, rest, restful, http-server
Open Rest
Standard rest server, Base on restify and sequelize
Stars: ✭ 136 (-46.25%)
Mutual labels:  api, rest-api, rest, restful-api
Blogbackendproject
Backend code for my blogs, develop with Django Rest framework.
Stars: ✭ 204 (-19.37%)
Mutual labels:  rest-api, rest, restful-api, restful
Restful Api Guidelines
A model set of guidelines for RESTful APIs and Events, created by Zalando
Stars: ✭ 1,397 (+452.17%)
Mutual labels:  api, rest-api, restful-api, restful
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-62.45%)
Mutual labels:  api, rest-api, restful-api, restful
Grafanajsondatasource
Grafana datasource to load JSON data over your arbitrary HTTP backend
Stars: ✭ 146 (-42.29%)
Mutual labels:  api, rest-api, rest, json
Codeigniter Jwt Sample
CodeIgniter JWT Sample
Stars: ✭ 144 (-43.08%)
Mutual labels:  rest-api, rest, restful-api, restful
Http restful api
整理HTTP后台端的RESTful API方面的知识
Stars: ✭ 94 (-62.85%)
Mutual labels:  api, rest-api, rest, restful
Smoke
💨 Simple yet powerful file-based mock server with recording abilities
Stars: ✭ 142 (-43.87%)
Mutual labels:  api, rest, json, mock
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (-2.77%)
Mutual labels:  api, rest-api, restful-api, restful
Appkernel
API development made easy: a smart Python 3 API framework
Stars: ✭ 152 (-39.92%)
Mutual labels:  api, rest-api, rest, restful-api

GitHub version Build Status Coverage Status Dependency Status devDependency Status Unicorn

http-fake-backend

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

It actually can serve the content of other file types as well as sending the files itself as response.

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 (v6.0.0 or greater)

Install

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

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.

Later (eg. for tests in CI)

npm start

Just starts the server via node.

Configure 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: '/response-files/simpleExample.json' }
        ]
    }]
});

Advanced Example

/server/api/anotherExample.js:

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

Serving different content types

/server/api/fileTypes.js:

module.exports = SetupEndpoint({
    name: 'fileTypes',
    urls: [{
        params: '/json',
        requests: [{
            response: '/response-files/simpleExample.json'
        }]
    }, {
        params: '/text',
        requests: [{
            response: '/response-files/example.txt',
            mimeType: 'text/plain'
        }]
    }, {
        params: '/html',
        requests: [{
            response: '/response-files/example.html',
            mimeType: 'text/html'
        }]
    }, {
        params: '/pdf',
        requests: [{
            response: '/response-files/example.pdf',
            sendFile: 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",
                //   "message" : "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: '/response-files/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 file:
      • response: '/response-files/articles.json'
    • Or just a JavaScript object:
      • response: { success: true }
  • urls.requests.mimeType
    • Optional (string). Defaults to application/json.
    • Is used to set the content-type response header.
  • urls.requests.sendFile
    • Optional (boolean). Defaults to false.
    • Sends the file as response instead of returning the file content.
  • urls.requests.statusCode
    • Optional (boolean). Defaults to 200
    • The HTTP status code of the response.
    • 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.

Configure server

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

# Custom response header
#CUSTOM_HEADER_NAME=Authorization
#CUSTOM_HEADER_VALUE=Bearer eyJhbGciOiJIUzUxMiJ9

Related

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].