0.1.5 • Published 5 years ago

express-hard-coded-response v0.1.5

Weekly downloads
158
License
MIT
Repository
github
Last release
5 years ago

Functions

Typedefs

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

ParamTypeDefaultDescription
optionsObject
options.conditionsArray.<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.verboseBooleanfalsetruthy-value to log each matched URL and corresponding pattern
options.baseDirObject"<empty-string>"Base directory for the relative paths
options.debugNoteObject"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.consoleObjectconsoleA logging/console object, which supports .log() and .warn()

ExpressMiddleware : function

Express middleware

Kind: global typedef

ParamTypeDescription
reqObjectRequest object
resObjectResponse object
nextfunctionNext 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

NameTypeDefaultDescription
patternString"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
statusString200Response status
typeStringType 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).
contentTypeString'Content-Type' header
responseTextStringThe text to be used as the hard-coded response (responseText has more priority than responseFile)
responseFileStringThe 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')