Quickly Set Up a No‑Mock, Regex‑Free API Proxy with Lowcode‑Mock

This guide shows how to install and run lowcode‑mock, create mock APIs with Koa, generate mock data from YAPI or JSON using the yapi‑code plugin, configure custom mock rules, simulate delays, handle HTTP error codes, and proxy requests to real back‑ends, all without needing regex‑based mocking.

Programmer DD
Programmer DD
Programmer DD
Quickly Set Up a No‑Mock, Regex‑Free API Proxy with Lowcode‑Mock

Installation

Run the following command to create a lowcode‑mock project:

yarn create @lowcoding/mock

Start the Server

Start the mock server with: yarn start The mock server listens on http://localhost:3000/ by default and supports CORS, so front‑end projects can request the mock service directly without additional proxy configuration.

Basic Mock Example

Create a JavaScript file under src/routes and add the following code:

import KoaRouter from 'koa-router'
import proxy from '../middleware/Proxy'
import { delay } from '../lib/util'
let Mock = require('mockjs')
let Random = Mock.Random

const router = new KoaRouter()
router.get('/your-mock-api', (ctx) => {
    ctx.body = '你的第一个mock接口'
})
module.exports = router

Generate Mock from YAPI

Copy the interface ID from YAPI (the numeric part at the end of the URL) and use the yapi-code VS Code extension to generate a mock endpoint and data automatically.

Generate Mock from JSON

Paste raw JSON data into the tool; it will generate a mock route (you need to adjust the route path manually). Example JSON:

const json = {
    code: 100,
    message: '请求成功',
    result: {
        list: [{
            code: '注意这是一个字符串的code',
            name: '张三',
            icon: '',
            actived: false
        }],
        total: 0
    }
}

The generated mock handler might look like this:

.get(`xxxxx`, async (ctx, next) => {
    const list1 = []
    for (let i = 0; i < 3; i++) {
        list1.push({
            code: Random.cword(5, 6),
            name: '模糊匹配后生成的mock',
            icon: Random.image('48x48'),
            actived: Random.boolean()
        })
    }
    ctx.body = {
        code: 200,
        message: '这是一条精确的mock',
        result: { list: list1, total: 200 }
    }
})

Custom Mock Rules (yapi‑code configuration)

Add the following configuration to package.json to customize mock data generation based on field names or values:

"yapi-code.mockKeyWordLike": {
    "icon": "Random.image('48x48')",
    "img": "Random.image('48x48')",
    "image": "Random.image('48x48')",
    "code": "200&&number",
    "name": "'模糊匹配后生成的mock'"
},
"yapi-code.mockKeyWordEqual": {
    "message": "'这是一条精确的mock'",
    "total": 200
},
"yapi-code.mockString": "Random.cword(5, 6)",
"yapi-code.mockBoolean": "Random.boolean()",
"yapi-code.mockNumber": "Random.natural(100,1000)"

Delay Simulation

import KoaRouter from 'koa-router'
import proxy from '../middleware/Proxy'
import { delay } from '../lib/util'
let Mock = require('mockjs')
let Random = Mock.Random

const router = new KoaRouter()
router.get('/delay', (ctx) => {
    delay(3) // return after 3 seconds
    ctx.body = 'delay'
})

This can be used to test loading states.

HTTP Error Status Codes

router.get('/httpError', (ctx) => {
    ctx.status = 401
    ctx.body = 'http 401'
})

Proxy Requests

router.get('/proxy', proxy('https://github.com/wjkang/lowcode-mock'), (ctx) => {
    ctx.body = 'https://github.com/wjkang/lowcode-mock'
})

router.all(new RegExp('^/lowcode/mock/(|^$)'), proxy('https://github.com/wjkang/lowcode-mock'))

These routes forward requests to the real back‑end, useful when the back‑end API is ready for integration.

Source Code

The full source code is available at https://github.com/wjkang/lowcode-mock .

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsKoaMock ServerAPI mockinglowcode-mockyapi-code
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.