docs/en/docs/rules/reqScript.md
Dynamically generate rules during the request phase using JavaScript to implement complex request processing logic. The script can access request context information and dynamically generate matching rules.
pattern reqScript://value [filters...]
| Parameters | Description | Detailed Documentation |
|---|---|---|
| pattern | Expression to match request URLs | Match Pattern Documentation |
| value | JS script to generate rules. Supports the following types: | |
| • Directory/File Path | ||
| • Remote URL | ||
| • Inline/Embedded/Values Content | Operation Instruction Documentation | |
| filters | Optional filters. Supports matching: | |
| • Request URL/Method/Header/Content | ||
| • Response Status Code/Header | Filter Documentation |
``` test.js
if (method === 'GET') {
rules.push('* resType://text');
rules.push('* file://(<div>GET-Request</div>)');
} else {
rules.push('* statusCode://403');
}
```
www.example.com/path reqScript://{test.js}
Accessing https://www.example.com/path/to returns:
| Variables/Methods | Description |
|---|---|
url | Full request URL |
method | Request method (GET/POST, etc.) |
ip/clientIp | Client IP address |
headers | Request header object |
body | Request body (max 16KB) |
rules | Rule array, add new rules via push |
values | Temporary value storage object |
render(tpl,data) | Micro template rendering function |
getValue(key) | Get the value in Values |
parseUrl | Same as url.parse in Node.js |
parseQuery | Same as querystring.parse in Node.js |