docs/docs/rules/file.md
file 协议用于将请求映射到本地文件系统,用本地文件的内容作为响应返回给客户端,适用于以下场景:
file 协议支持多种方式指定本地文件内容:
直接在规则中写明要返回的内容,适用于简短的文本内容。注意:value 不能包含空格和换行符。
pattern file://(value) [lineProps...] [filters...]
示例:
www.example.com/api/data file://({"status":"ok"})
注意:内联值必须用括号 () 包裹,否则会被识别为本地文件路径,可能导致 404 错误:
# 正确写法:返回字符串 "success"
pattern file://(success)
# 错误写法:会被当作路径处理,可能导致 404
pattern file://success
file 协议的内联值语法要点:
- 必须使用
()包裹内容- 内容不能包含空格和换行符
- 可以是字符串、JSON、HTML片段等
- 如果不加
(),Whistle 会将其解释为文件路径或{key}并尝试加载对应文件或 key 值
当需要处理包含空格、换行符的复杂内容,或希望复用某段配置时,推荐使用此方式。
pattern file://{custom-key.json} [lineProps...] [filters...]
``` custom-key.json
{
"status": "success",
"data": {
"message": "Hello from local file"
}
}
```
最佳实践:建议为 key 添加响应类型后缀(如 .json、.html、.css),Whistle 会自动根据后缀设置正确的 Content-Type。
当内容较大时,可以将其存储在 Values 配置区中。
pattern file://{key-of-values.html} [lineProps...] [filters...]
前提:在 Values 中存在名为 key-of-values.html 的键,其值为要返回的内容。
容量建议:
Values 中当需要频繁编辑内容时,可以使用 Whistle 提供的临时文件功能。
pattern file://temp.html
操作步骤:
Command(Mac)/ Ctrl(Windows)file://temp.htmlSave 保存保存后规则会自动变为类似以下格式:
https://example.com/report file://temp/11adb9c9e1142df67b30d7646ec59bcd34c855d9011d1a2405c7fc2dfc94568d.html
需要再次编辑时,用同样的方式点击该临时文件链接即可。
从本地文件系统或远程 URL 加载响应内容。
# 从本地文件加载
pattern file:///User/xxx/test.html
# 从远程 URL 加载
pattern file://https://example.com/template.html
| 参数 | 是否必填 | 描述与示例 |
|---|---|---|
| pattern | 是 | 用于匹配请求 URL 的表达式。 |
| • 支持域名、路径、通配符、正则表达式。 | ||
| • 详见 匹配模式文档。 | ||
| value | 是 | 要返回的文件内容或路径,支持多种格式: |
| • 本地文件或目录路径 | ||
| • 远程 URL | ||
| • 内联、内嵌、Values 引用内容 | ||
| lineProps | 否 | 为规则设置附加属性。 |
| 示例: | ||
• lineProps://important:提升规则优先级 | ||
• lineProps://weakRule:当同时配置 file 和 proxy 规则时,默认 proxy 会失效。通过此属性可提升 proxy 规则的优先级。 | ||
| • 详见 lineProps 文档。 | ||
| filters | 否 | 可选的过滤条件,用于精确控制规则生效的场景。 |
| • 可匹配请求的 URL、方法、头部、体内容。 | ||
| • 可匹配响应的状态码、头部。 | ||
| • 详见 过滤器文档。 |
# 将域名映射到本地目录
www.example.com/path file:///Users/username/projects/my-site
# Windows 系统路径
www.example.com/path file://D:\projects\my-site
特性说明:
https://www.example.com/path/x/y/z 会映射到 /Users/username/projects/my-site/x/y/z< > 包裹路径可以禁用自动拼接
www.example.com/path file://</Users/username/projects/my-site/index.html>
访问
https://www.example.com/path/x/y/z只会返回/Users/username/projects/my-site/index.html文件
# 内联方式
www.example.com/jsonp file://`(${query.callback}({"status":"ok"}))`
# 内嵌方式
``` jsonp-response
${query.callback}({
"status": "error",
"message": "Invalid parameter"
})
```
www.example.com/jsonp file://`{jsonp-response}`
模板字符串限制:模板字符串在以下场景无法直接生效:
- 引用本地文件路径时
- 引用远程 URL 地址时
遇到上述限制时,可以使用 tpl 协议作为替代方案。
# 排除特定接口
www.example.com/api/path file:///Users/username/mock-data excludeFilter://*/api/auth
# 根据请求体内容匹配
www.example.com/api/search file:///Users/username/search-results.json includeFilter://b:/"type":\s*"advanced"/i
# 仅匹配 POST 请求
www.example.com/api/submit file:///Users/username/success.json includeFilter://m:POST
# 多个文件或目录用 `|` 分隔
www.example.com/static/path file:///path/to/project1|/path/to/project2|/path/to/project3
查找逻辑:
/path/to/project1/static/file.js/path/to/project2/static/file.js/path/to/project3/static/file.js404 Not Found# 使用正则表达式捕获组
/^https?://www\.example\.com/user/(\d+)/profile file:///Users/username/mock/profiles/user-$1.json
# 如果不限制数字,可以用通配符
^www.example.com/user/*/profile file:///Users/username/mock/profiles/user-$1.json
``` dev-config
{
"apiBase": "http://localhost:3000",
"debugMode": true
}
```
``` prod-config
{
"apiBase": "https://api.example.com",
"debugMode": false
}
```
# 通过 cookie:env=dev 判断是否为开发环境
www.example.com/config file://{dev-config} includeFilter://reqH:cookie=/env=dev/
www.example.com/config file://{prod-config} # 默认正式环境
# 先使用 file 提供静态文件,再设置响应头
www.example.com file:///Users/username/static-files cache://3600
# 对于不存在的文件,使用 xfile 允许请求继续
www.example.com xfile:///Users/username/static-files
file 协议与 resBody 协议的主要区别在于请求处理流程和响应机制:
file 协议:*
file 协议的请求不会发送到后台服务器示意图:
客户端请求 → Whistle (匹配file规则) → 读取本地文件 → 返回响应 (200)
↓
(不发送到服务器)
resBody 协议:
示意图:
客户端请求 → Whistle → 发送到服务器 → 收到原始响应 → 替换响应体 → 返回修改后的响应
允许未匹配文件继续访问:xfile
file://https://xxx 这种形式域名解析重定向:host
模板渲染:tpl
~ 表示用户主目录/ 和 \ 作为路径分隔符A: 检查:
< > 禁用了路径拼接A: 检查:
Content-Type 是否正确A: 检查:
A: 检查: