All Projects → 52cik → express-mockjs

52cik / express-mockjs

Licence: MIT license
mockjs api middleware for Express

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to express-mockjs

Vue2.x-mobileSystem
基于Vue2.0的移动端项目,项目没有使用vue-cli,全部手写,让小白更容易学习理解
Stars: ✭ 72 (+53.19%)
Mutual labels:  mockjs
vue-mintUI-demo
采用vue2、Mint UI,做的移动端项目
Stars: ✭ 17 (-63.83%)
Mutual labels:  mockjs
ohana
A simple server that return mock data for test
Stars: ✭ 31 (-34.04%)
Mutual labels:  mockjs
ECHI VUE CLI3.0
[email protected] + axios + mock + typescript + element-ui + scss + webpack4构建的企业级应用项目基础模板;
Stars: ✭ 15 (-68.09%)
Mutual labels:  mockjs
vue-lite-admin
a lite vue3.0 admin template,there is no typescript and vuex (但注释挺全)
Stars: ✭ 67 (+42.55%)
Mutual labels:  mockjs
Vue Todos
vue最新实战项目教程,从零开始,一步一个脚印,循序渐进。跟着我一起学习vue吧!
Stars: ✭ 1,659 (+3429.79%)
Mutual labels:  mockjs
dva-typescript-antd-starter-kit
A admin dashboard application demo based on antd by typescript and dva
Stars: ✭ 61 (+29.79%)
Mutual labels:  mockjs
crm
客户关系管理系统
Stars: ✭ 53 (+12.77%)
Mutual labels:  mockjs
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+197.87%)
Mutual labels:  mockjs
vue-antd-admin
基于vue3.0 + vue-cli4.0 + vue-router4.x + vuex4.x + ant-design-vue2.x开发的后台管理系统模板,包含权限路由、权限按钮、流程配置、个人中心等基础功能
Stars: ✭ 57 (+21.28%)
Mutual labels:  mockjs

express-mockjs

express mockjs api middleware for Express

Linux Build Coverage Status Dependencies node license MIT

中文文档

How to use it

Installation

$ npm install --save-dev express-mockjs

Quick start

  1. Create a directory api-server, then create the file app.js, the content is:
var path = require('path')
var express = require('express')
var mockjs = require('express-mockjs')

var app = express()

// Use the default path '/' (Not recommended)
// app.use(mockjs(path.join(__dirname, 'mocks')))

// Use a custom path '/api'
app.use('/api', mockjs(path.join(__dirname, 'mocks')))

// Here you can add any code.

app.listen(3000);
  1. Create a mocks directory under api-server and create data.json as follows:
/**
 * api interface description
 *
 * @url /test-api
 */
{
  "code": 0,
  "result|5": [
    {
      "uid|+1": 1,
      "name": "@name",
      "email": "@email"
    }
  ]
}
  1. Install dependent modules
$ npm i -S express express-mockjs
  1. Start
$ node app.js
# or
$ nodemon app.js

You can then access the http://localhost:3000/api to view API documents.

Recommended using nodemon to monitor auto restart services


Mock JSON

Examples

.
├── mocks
    ├── home
    ⎪   ├── data.json
    ├── user
    ⎪   ├── data.js
    ⎪   ├── data.json
    ├── game
        ├── data.json

data.json

Mock JSON here is not a real JSON file, and more like a JS file, so you want to use the following format.

Hypothetical file in 'mocks/home/test.json'

/**
 * Interface function description
 *
 * @url /api-access-path
 *
 * Parameter description and other instructions.
 * uid: user ID
 * name: username
 * email: the email
 * etc.
 */

{
  "code": 0,
  "result|5": [
    {
      "uid|+1": 1,
      "name": "@name",
      "email": "@email"
    }
  ]
}

Then you can access the http://localhost:3000/api/api-access-path through the browser.

Of course, you can also use the JS file directly.

/**
 * home page links
 *
 * @url /home-links
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 */

module.exports = {
  "code": function () { // simulation error code, 1/10 probability of error code 1.
    return Math.random() < 0.1 ? 1 : 0;
  },
  "list|5-10": [
    {"title": "@title", "link": "@url"}
  ]
};

Or export function.

/**
 * user page - user info
 *
 * @url /user?uid=233
 *
 * GET: Request method and parameter
 *   uid This is the requested userID
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 */

module.exports = function (req) {
  var uid = req.query.uid;

  if (!uid) {
    return {
      code: -1,
      msg: 'no uid',
    }
  }

  return {
    code: 0,
    data: {
      "uid": +uid,
      "name": "@name",
      "age|20-30": 1,
      "email": "@email",
      "date": "@date",
    },
  };
};
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].