/express-hard-coded-response

Express middleware to return hard-coded response for requests with url matching specified pattern

Primary LanguageJavaScriptMIT LicenseMIT

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

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 - Express middleware

Param Type Default Description
options Object
[options.conditions] Array.<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'])