Giter Club home page Giter Club logo

express-mockjs's Introduction

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",
    },
  };
};

express-mockjs's People

Contributors

52cik avatar 90arther avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.