UNPKG

tools-mock

Version:

A node.js mock tool to quickly start the server via http

92 lines (88 loc) 2.27 kB
import CMockConfig from './CMockConfig'; import CMockSer from './CMockSer'; /** * 快速启动http服务工具 * @example * import { CMock } from 'tools-mock' const cmock = new CMock() cmock.setConfig({ resAfter(param) { //处理当存在返回的数据是html时修改Content-Type,并且将回车符、表格符、换行符修改?还原? let endStr = param.data let isHtml = endStr.indexOf('<!DOCTYPE') isHtml = isHtml < 20 && isHtml >= 0 if (isHtml) { param.headers['Content-Type'] = 'text/html; charset=utf-8' endStr = endStr.substring(1, endStr.length - 1) endStr = endStr.replace(/\\\"/g, '"').replace(/\\r/g, '\r').replace(/\\n/g, '\n').replace(/\\t/g, '\t') } param.data = endStr }, staticFileUrl(url) { //将根转发到index.html if (url == '/') url = '/index.html' return url } }) cmock.setStaticDir('D:/html') cmock.setData({ '/': { method: 'GET', fun: () => { return 'Hello World' } } }) cmock.run(2922) */ export default class CMock { server: CMockSer; config: CMockConfig; constructor(); /** * 设置静态资源地址 * @param dir */ setStaticDir: (dir: string) => void; /** * 设置配置 * @param config */ setConfig: (config: CMockConfig['config']) => void; /** * 设置接口数据 * @param data */ setData: (data: CMockConfig['config']['data']) => void; /** * 设置websocket接口数据 * @param data * @example * { '/gameServer': { connection({ req, ws }) { console.log('connection gameServer') }, message({ message, ws }) { console.log('message') } }, '/api/mini/games/#{token}/#{miniGame}': { connection({ req, ws }) { console.log('connection') }, message({ message, ws }) { console.log('message') } } } */ setWSData: (data: CMockConfig['wsconfig']['data']) => void; /** * 运行-每次运行会停止之前的并重新加载配置 * @param port */ run: (port: number) => Promise<any>; }