Mock services for building APIs using Nodejs

Source: Internet
Author: User

1. Introduction

The company's business development are static pages, the development of the early days always avoid the problem of getting the API. There are some mockjs scenarios in Vue, the scenarios are injected, and the final deployment is always different, and the business is mostly under Zepto, and it's hard to find the right solution. While the development of the pre-adjustment CSS style requires real DOM, not always write a static page to debug. Just use the convenience of KOA2, build a mock service.

Online mock service is still quite a lot of, most of the URL path to differentiate API name, for the common API is sufficient, but for the input parameters as the API name, there is no way to. For example, our company's API:

@url/api@params {   "MyApi",   ' {' name ': ' 3333 '} '}

This strange API simply can't be achieved with ordinary mock-up ...

So I developed a mock service, GitHub address: https://github.com/wenlonghuo/xmocker-cli. It can also be installed with NPM: NPM i-g xmocker-cli. Preview Interface Address: http://xmocker.wlhot.top/#/about

The objectives of the design are:

1. Local modification without prejudice to others

2. Can open an agent, when the local data is insufficient, the API request forwarded to the other server

3. Easy to install, interface operation.

2 implementation


The framework is developed using KOA2 and Babel conversion code facilitates support for NODEJS version 6. Localize the database, as long as a NPM installation is used directly.

The overall structure is:

API Branch, API basic information, project

After adding the project, the API, can operate on the interface to start, restart, etc., also provides the command-type start file server.

2.1 Structure

Project: Name, path, port, proxy

API Base Information: Name, method, URL, two-level path, two-level path corresponding value

API Branch: Name, judging condition, data

2.2 Implementation of different API distinctions

Register KOA's middleware on all router, use Koa-bodyparse to get post-related data, and consolidate all input parameters:

  

Object.assign ({}, Ctx.params, Ctx.query | | {}, Ctx.request.body)

The API base Information list is then traversed to determine if the specified key value is equal to the predetermined value, and the API to which the request belongs

2.3 Two-level differentiation of APIs

For more flexible data returns, you can add only custom functions. Add the judgment condition in the API configuration, generate the function by the new function form, judge by the return value, if it is not true, then detect the next branch, otherwise determine the current branch is the target value, get the mock data, request return

2.4 Implementation of the agent

Implement the proxy using the Http-proxy module. For ease of use, two forms of proxy are required: Specify the API path proxy to the server and the agent to the server when the API is not found. The former is very simple, the use of Koa-router Direct registration to add middleware, the latter need to use the KOA onion ring model, after the request to determine the HTTP status of 404, and then proxy to the specified server

2.5 Opening the file server

Light has API service not, after all, I developed a static page, the need for a file service, easy to write edge tune. Using the Koa-send module to implement file services, using NPM link or npm install will be in the Global Registration command, convenient to open the file service.

2.6 Interface Services

Using the framework for rapid development, use the iview framework, VUE-CLI build scaffolding, build dist files, use file services, listen to 6001 ports, browser access can control the service.

Preview interface See: Http://xmocker.wlhot.top/#/about

3 Use

With NPM link, you can register a global command, and a simple mocker start service. Of course, the follow-up has added some practical functions, stability and the like still need to be optimized, at least in the development speed of the promotion is relatively large. Since the company does not write test cases, finish finishing the page, this set of API Services has become our company's front-end development of the necessary tools, at least Mac and Windows can run normally, but also no time to write (anyway, just practiced hand). Welcome to use this set of tools, what problems can be mentioned on GitHub issue, there is an interaction to update the power.

Mock services for building APIs using Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.