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

express-hot-mock-middleware

v1.0.2

Published

an express middleware for implementing mock api with reference to umi-mock

Downloads

4

Readme

express-hot-mock-middleware

express-hot-mock-middleware 是一个参考了 umijs 实现了其类似的基于 express 的 mock 中间件,可以方便 react 项目集成

使用时只需要指定一个到处接口数据的目录,支持实时检测目录下文件的改动并动态加载

Installation

npm install --save-dev express-hot-mock-middleware

Usage

  1. 使用 create-react-app 创建一个 react app

  2. src 目录下创建文件 setupProxy.js

setupProxy.js 内容如下:

require('@babel/register');
const fs = require('fs');
const path = require("path");
const proxy = require('http-proxy-middleware');

const appPath = fs.realpathSync(process.cwd());

module.exports = function (app) {

    const mockPaths = [
        path.join(appPath, 'mock')
    ];

    app.use(require("express-hot-mock-middleware").createMiddleware(mockPaths));
};
  1. 在项目根目录下创建 babel.config.js 内容如下:
module.exports = function (api) {
    api.cache(true);

    const presets = [
        "react-app",
        [
            // https://babeljs.io/docs/en/babel-preset-env#usebuiltins
            "@babel/preset-env",
            {
                "useBuiltIns": "usage",
                "corejs": "3"
            }
        ],
        // ...
    ];
    const plugins = [
        // ...
    ];

    return {
        presets,
        plugins
    };
}
  1. 在项目根目录下创建 mock 目录,然后在此目录下创建一个 api.js 作为示例:
import mockjs from 'mockjs';

const getNotice = [
    {
        id: 'xxx1',
        title: titles[0],
        logo: avatars[0],
        description: '那是一种内在的东西,他们到达不了,也无法触及的',
        updatedAt: new Date(),
        member: '科学搬砖组',
        href: '',
        memberLink: '',
    },
    {
        id: 'xxx2',
        title: titles[1],
        logo: avatars[1],
        description: '希望是一个好东西,也许是最好的,好东西是不会消亡的',
        updatedAt: new Date('2017-07-24'),
        member: '全组都是吴彦祖',
        href: '',
        memberLink: '',
    },
    // ...
];

const getActivities = [
    {
        id: 'trend-1',
        updatedAt: new Date(),
        user: {
            name: '曲丽丽',
            avatar: avatars2[0],
        },
        group: {
            name: '高逼格设计天团',
            link: 'http://github.com/',
        },
        project: {
            name: '六月迭代',
            link: 'http://github.com/',
        },
        template: '在 @{group} 新建项目 @{project}',
    },
    {
        id: 'trend-2',
        updatedAt: new Date(),
        user: {
            name: '付小小',
            avatar: avatars2[1],
        },
        group: {
            name: '高逼格设计天团',
            link: 'http://github.com/',
        },
        project: {
            name: '六月迭代',
            link: 'http://github.com/',
        },
        template: '在 @{group} 新建项目 @{project}',
    },
    // ...
];

function getFakeCaptcha(req, res) {
    return res.json('captcha-xxx');
}

export default {
    'GET /api/project/notice': getNotice,
    'GET /api/activities': getActivities,
    'POST /api/forms': (req, res) => {
        res.send({message: 'Ok'});
    },
    'GET /api/tags': mockjs.mock({
        'list|100': [{name: '@city', 'value|1-100': 150, 'type|0-2': 1}],
    }),
    // ...
};

License

MIT Copyright (c) 2019