前端开源库-http-hash-mocker

  • a4_563528
    了解作者
  • 7.5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-23 08:00
    上传日期
前端开源库-http-hash-mockerHTTP哈希模拟程序,使用HTTP哈希创建模拟数据
前端开源库-http-hash-mocker.zip
内容介绍
http-hash-mocker ==== http-hash-mocker create request handler ```jsig import { HttpRequest, HttpResponse } from "node.http" type RoutePattern : String type MockerOpts : { basedir: String, rootdir?: String, locator?: String, routes?: Array<RoutePattern>, autoGenerate?: Boolean, template?: String, } type Router : ( req: HttpReqest, res: HttpResponse, opts: Object, cb: Callback<Error, void> ) => void type Mocker : { router: HttpHashRouter } & Router http-hash-mocker : (mopts: MockerOpts) => Mocker ``` ```js const http = require('http'); const path = require('path'); const sendError = require('send-data/error'); const mocker = require('http-hash-mocker')({ basedir: path.resolve(__dirname, '../') // rootdir: '/', // locator: 'test/fixtures', routes: [ '/api/photo/:photoid' ], autoGenerate: true, template: ` module.exports = { statusCode: 200, body: 'hello data template', }; `, }); const server = http.createServer(function (req, res) { mocker(req, res, {}, function (err) { if (err) { if (!res.finished) { sendError(req, res, { body: err }); } } }); }); ``` test/fixtures/api/photo/200.js ```js module.exports = { statusCode: 200, body: 'OK', }; ``` test/fixtures/api/photo/:photoid.js ```js const sendPlain = require('send-data/plain'); module.exports = function (req, res, opts, cb) { sendPlain(req, res, 'Nine Nine Nine', cb); }; ``` you mock it.
评论
    相关推荐