https://github.com/webextensions/express-hard-coded-response
Express middleware to return hard-coded response for requests with url matching specified pattern
https://github.com/webextensions/express-hard-coded-response
Last synced: over 1 year ago
JSON representation
Express middleware to return hard-coded response for requests with url matching specified pattern
- Host: GitHub
- URL: https://github.com/webextensions/express-hard-coded-response
- Owner: webextensions
- License: mit
- Created: 2017-02-27T07:51:48.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-07-19T18:04:09.000Z (almost 7 years ago)
- Last Synced: 2025-02-23T14:47:43.458Z (over 1 year ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Functions
-
hardCodedResponse(options) ⇒ExpressMiddleware -
This function is an Express JS middleware which would try to match the specified pattern (from an array of MatchCondition objects)
with req.originalUrl.
- If no match is found, then the request would just pass through.
- If a match is found, then serve the corresponding file (specified in the MatchCondition object) as its hard-coded response.
Note: res.locals['matchedCondition'] is set as per the first-matched-condition
## Typedefs
-
ExpressMiddleware :function -
Express middleware
-
MatchCondition :Object -
The condition to be matched (.pattern is used for match with request and other attributes are used for response)
## hardCodedResponse(options) ⇒ [ExpressMiddleware](#ExpressMiddleware)
This function is an Express JS middleware which would try to match the specified pattern (from an array of MatchCondition objects)
with req.originalUrl.
- If no match is found, then the request would just pass through.
- If a match is found, then serve the corresponding file (specified in the MatchCondition object) as its hard-coded response.
Note: res.locals['matchedCondition'] is set as per the first-matched-condition
**Kind**: global function
**Returns**: [ExpressMiddleware](#ExpressMiddleware) - Express middleware
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object | | |
| [options.conditions] | [Array.<MatchCondition>](#MatchCondition) | | Array of conditions which would be used for matching with req.originalUrl
A falsy-value (or empty array) would make this function call effectively empty and the request would pass-through |
| [options.verbose] | Boolean | false | truthy-value to log each matched URL and corresponding pattern |
| [options.baseDir] | Object | "<empty-string>" | Base directory for the relative paths |
| [options.debugNote] | Object | "This is a hard-coded response intended for debugging purposes only" | Debug note to be added as HTTP header (and JSON property if MatchCondition says .type is 'json')
- If responseText & responseFile are not provided in MatchCondition, then debugNote is used as the response
- Set it to false to disable it |
| [options.console] | Object | console | A logging/console object, which supports .log() and .warn() |
## ExpressMiddleware : function
Express middleware
**Kind**: global typedef
| Param | Type | Description |
| --- | --- | --- |
| req | Object | Request object |
| res | Object | Response object |
| next | function | Next function |
## MatchCondition : Object
The condition to be matched (.pattern is used for match with request and other attributes are used for response)
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| pattern | String | | "none" or falsy-value would match none
"*" or "all" would match all
"<any-other-pattern>" would be searched as plain string anywhere in the req.originalUrl |
| status | String | 200 | Response status |
| type | String | | Type of response (currently supporting 'json')
If 'json' is used:
- responseFile's contents would be read as commented-json (CJSON) and the commentes would be stripped-off.
- 'Content-Type' header is set as 'application/json; charset=utf-8' by default, unless overridden using contentType option.
- debugNote would be added as a property to the JSON response (this can be overwritten through the options when setting up the middleware). |
| contentType | String | | 'Content-Type' header |
| responseText | String | | The text to be used as the hard-coded response (responseText has more priority than responseFile) |
| responseFile | String | | The file to be used as the hard-coded response (responseText has more priority than responseFile) |
| Any-other-object-properties | \* | | Any other object properties (these might be accessed after match from res.locals['matchedCondition']) |