npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

h-mock-middleware

v0.6.1

Published

mock-middleware

Downloads

27

Readme

h-mock-middleware - mock中间件

注意事项

此中间件支持 Express app.use(req,res,next) 和 koa app.use(ctx,next) 的调用,但 mockSettings.js 中,仅支持类 Express 的 (req,res,next) 形式的中间件 handle 。

使用方法

配置文件

配置文件mockSettings.js支持修改立即生效。支持js函数执行。

// mockSettings.js
mockSettings = {
    "delay": 0, // 全局延时
    "timeout": 10000, // 请求超时设置
    "temDir": "tem", // 自动记录requestRemote的结果目录
    "dist_dir": {
        "js": "dist-prod/js"
    },
    "mockList": [{
        //     'reg': '/api/controller/action', // 匹配的正则表达式
        //     'handle': ajaxSync('./path/to/mockdata.json') // 发送对应json文件
        // }, {
        'reg': '/api',
        'handle': requestRemote('http://a.b.c') // 代理到对应的远端
        // }, {
        //     'reg': '/api',
        //     'handle': requestRemote('https://publish.online.url/')
        // }, {
        //     'reg': '/data/',
        //     'handle': ajaxDirSync({dirBasePath: './data/', pathPrefix: '/data/'}) // 返回文件,将url的前缀用pathPrefix替换,并返回相对于dirBasePath地址的文件。
    }, {
        'reg': '^/dist/',
        'handle': routerReplace('^/dist/', '/') // 替换router url的指定正则表达式部分为指定内容
    }, {
        'reg': '/data/',
        'handle': ajaxDirSync() // 默认参数{dirBasePath: './',pathPrefix: '/'}
    }]
}

Express 实例用法

以webpack开发服务为例, devServer.before 第一参数是 Express app 实例。

// webpack.config.js
var middewareFun = require('h-mock-middleware').middleware;
module.exports = {
    //...
    devServer: {
        before: function(app, server) {
            app.use(middewareFun());
        }
    }
};

webpack运行目录中放置配置文件mockSettings.js(见上文)

gulp 工作流

gulpfile.js文件中:

// gulpfile.js
var middewareFun = require('h-mock-middleware').middleware;
gulp.task('serve', function() {
    browserSync.init({
        port: 3000,
        server: {
            baseDir: "./",
        },
        middleware: [middewareFun()],
    });
}

gulpfile.js文件同级目录中放置配置文件mockSettings.js(见上文)

其他事项

为了使配置文件mockSettings.js可以使用js注释语法,代码中使用了eval,请在使用本包的时候,注意mockSettings.js需是可信的。
修改配置文件,不必重启进程,在下一次请求就会生效。
未配置temDir时,请求远端数据不会自动记录。
配置文件中可以使用js注释语法。
handle 函数抛出异常,会返回500状态码和异常信息。 handle 不调用next就会由当前handle处理;调用next会继续检查下一个mockItem。 遍历mockList时,最后一个handle调用next会转移控制权到其他中间件。 在 koa use中, handle 调用 req.status 时,会设定 response.status,并且之后调用 req.end 时,不会被改成 200 。 在 koa use中, handle 调用 req.end 时,若未设定 status ,将自动默认设定 200 ,并且会结束本中间件(resolve())。

仓库地址

TODO

  • English translation.
  • 完善设置功能。
  • 整理代码。