Installations
npm install webpack-mock-service
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
12.18.2
NPM Version
6.14.5
Score
70.4
Supply Chain
95.8
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (90.34%)
JavaScript (9.66%)
Developer
ifcheng
Download Statistics
Total Downloads
996
Last Day
1
Last Week
2
Last Month
14
Last Year
91
GitHub Statistics
1 Stars
14 Commits
1 Watching
7 Branches
1 Contributors
Bundle Size
3.74 kB
Minified
1.47 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0
Package Id
webpack-mock-service@3.0.0
Unpacked Size
15.02 kB
Size
5.86 kB
File Count
14
NPM Version
6.14.5
Node Version
12.18.2
Total Downloads
Cumulative downloads
Total Downloads
996
Last day
0%
1
Compared to previous day
Last week
-80%
2
Compared to previous week
Last month
100%
14
Compared to previous month
Last year
-65.4%
91
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
webpack-mock-service
提供api mock服务,可配合webpack-dev-server
使用,也可单独使用
V3版本开始不再需要提供入口文件,程序会自动导入mock文件夹下符合条件的文件
安装
$ npm install webpack-mock-service
使用
webpack.config.js
1const path = require('path') 2const MockService = require('webpack-mock-service').default 3 4module.exports = { 5 // ... 6 devServer: { 7 // ... 8 before(app) { 9 new MockService(app, { 10 directory: path.join(process.cwd(), 'mock'), // default 11 excludeApis: '/exclude', 12 baseUrl: '/api' 13 }) 14 }, 15 // '/api/exclude'和'/api/nomatch'将会被转发到'http://api.example' 16 proxy: { 17 '/api': { 18 target: 'http://api.example', 19 changeOrigin: true 20 } 21 } 22 } 23}
mock/index.js
1module.exports = { 2 'GET /test': { 3 delay: 1000, 4 response: { 5 message: 'Response from mock service.' 6 }, 7 }, 8 'POST /exclude': { 9 response(req, res) { 10 res.send('Response from mock service.') 11 }, 12 }, 13}
参数说明
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文件的缓存,确保拿到最新数据。
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/14 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
22 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More