mockstarjs
  • 关于 MockStar
  • 安装和升级
  • 快速上手
  • 非 CLI 方式
  • 技术细节
    • 基本概念
    • 整体架构
    • 为什么需要数据模拟
    • 如何做数据模拟
  • 配置
    • mockstar.config.js 配置
    • mocker 的 config.json 配置
    • mock module 的 config.json 配置
Powered by GitBook
On this page

Was this helpful?

非 CLI 方式

Previous快速上手Next基本概念

Last updated 6 years ago

Was this helpful?

有些场景下不适合使用 CLI 命令来启动 mock server,MockStar 提供来支持。

最简单的例子详见

如果不需要停止服务,删除 setTimeout 中的代码即可。

const path = require('path');
const mockstarLocalServer = require('mockstar-local-server');

// 服务启动参数
const configOpts = {
    rootPath: __dirname,
    mockServerPath: path.resolve(__dirname, '../../../mockstar/test/data/fixtures/mock_server/mockers'),
    watch: true
};

// 启动本地服务
const runServer = mockstarLocalServer.startServer(configOpts, (isSuccess, data) => {
    console.log('startServer callback', isSuccess, data);
});

// 3s 之后停止服务
setTimeout(() => {
    runServer.stop(() => {
        console.log('stop server success!');
    });
}, 3000);
mockstar-local-server
https://github.com/mockstarjs/mockstar/blob/master/packages/mockstar-local-server/demo/01/index.js