Socket
Book a DemoInstallSign in
Socket

back-mock

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

back-mock

Used to simulate the backend API interface

1.0.5
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

#back-mock

这个一个模拟后端api数据交互的模块。

##安装

npm install back-mock

##使用

新建一个文件(假如命名为mock.js)。在里面写入如下代码。

var mockServer = require("back-mock");
var path = require("path");

mockServer('0.0.0.0', 2000, path.resolve('./mocks'));

然后在mock.js的同目录新建一个mocks目录,在mocks目录里新建一个文件api.js。api.js里的内容是如下

exports.api_test = {
  path: '/api/test',
  POST: function() {
    return {
      xxoo: "this is post"
    };
  },
  GET: function() {
    return {
      xxoo: "this is get"
    }
  }
};

exports.api_comptest = {
  path: '/api/love',
  GET: function(req){
    if (req.query.name == "girl"){
      return "yes"
    }
    else {
      return "no"
    }
  }
}

GET和POST方法里的req是express里的req,具体使用可以参考express

现在就是现在,运行一下mock.js。

node mock.js

就可以访问

http://127.0.0.1/api/test

查看结果。

http://127.0.0.1/api/love?name=girl

{"data":"yes","status":"ok","description":"no error"}

mocks目录里可以放很多.js文件,只要格式和api.js文件一样就行,还要保证各个文件的path参数不一样。

##tips 启动mock.js时,推荐使用supervior来启动,这样修改文件后不用重启服务,supervior会自动重新启动。

Keywords

mock

FAQs

Package last updated on 31 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.