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

webpack-mock-service

v3.0.0

Published

Mocks api requests for webpack-dev-server

Downloads

4

Readme

webpack-mock-service

提供api mock服务,可配合webpack-dev-server使用,也可单独使用

V3版本开始不再需要提供入口文件,程序会自动导入mock文件夹下符合条件的文件

安装

$ npm install webpack-mock-service

使用

webpack.config.js

const path = require('path')
const MockService = require('webpack-mock-service').default

module.exports = {
  // ...
  devServer: {
    // ...
    before(app) {
      new MockService(app, {
        directory: path.join(process.cwd(), 'mock'), // default
        excludeApis: '/exclude',
        baseUrl: '/api'
      })
    },
    // '/api/exclude'和'/api/nomatch'将会被转发到'http://api.example'
    proxy: {
      '/api': {
        target: 'http://api.example',
        changeOrigin: true
      }
    }
  }
}

mock/index.js

module.exports = {
  'GET /test': {
    delay: 1000,
    response: {
      message: 'Response from mock service.'
    },
  },
  'POST /exclude': {
    response(req, res) {
      res.send('Response from mock service.')
    },
  },
}

参数说明

MockOptions

Param | Type | Default | Description -------------- | ------------------------------- | ----------- | ------------ directory | string | path.join(process.cwd(), 'mock') | mock文件夹绝对路径 useSubdirectories | boolean | true | 是否使用子目录 filter | RegExp\|((filepath: string) => boolean) | /\.js(on)?$/ | 文件过滤器 watchPaths | string\|string[] | - | 需要监测变化的文件/文件夹,作为第一个参数传递给chokidar.watch ,默认为directory的值 watchOptions | object | - | 作为第二个参数传递给chokidar.watch middlewares | express.Handler[] | - | 中间件 baseUrl | string | / | api基础路径 includeApis | string\|RegExp\|Array<string\|RegExp> | * | 包含的api接口 excludeApis | string\|RegExp\|Array<string\|RegExp> | - | 排除的api接口 fallthrough | boolean | true | 没有匹配到api接口时,是否把请求交给下一个中间件处理 updateDelay | number | 2000 | 文件改动后更新mock服务的延迟时间(ms),用于防抖。此外,很多编辑器在保存的时侯是先把原文件清空,再进行保存,因此会触发2次文件改变事件,设置该值也可以解决这个问题

MockResponse

Key | Type | Default | Description --------------| --------------------------------------------------- | ------ | ------------------------------- response | unknown[]\|Record<string, unknown>\|express.Handler| - | 响应数据或处理请求的函数 delay | number | - | 响应等候时间(ms) status | number | 200 | HTTP状态码

热更新

默认情况下,监测到文件变化时只会清除改动文件本身的缓存。当这个文件不会影响到其他mock文件的输出时,这么做是没问题的。否则,务必使文件名以_开头,这样会清除所有已加载的mock文件的缓存,确保拿到最新数据。