Collection of helper functions for use with the nodejs express web framework
- Source:
- Copyright:
- 11/2/19
Methods
(async, inner) asyncPassportAuthenticate(authOptions, provider, providerNiceLabel, req, res, next, jrResult)
- Source:
- See:
Async Wrapper around the express/passport passport.authenticate function
The passport library uses callbacks instead of promises and aync functions, so we wrap it here in a function that let's us use it like an async function for ease of use.
Parameters:
Name | Type | Description |
---|---|---|
authOptions |
* | |
provider |
* | |
providerNiceLabel |
* | |
req |
* | |
res |
* | |
next |
* | |
jrResult |
* | our JrResult class with info about success or errors; pass in a valid instance and it is updated |
Returns:
a simple object with passport user data
(async, inner) asyncPassportReqLogin(userPassport, errorMessage, req, jrResult)
- Source:
- See:
Async Wrapper around the express req.logIn function
The express library uses callbacks instead of promises and aync functions, so we wrap it here in a function that let's us use it like an async function for ease of use.
Parameters:
Name | Type | Description |
---|---|---|
userPassport |
* | simple object with user data to log in the user |
errorMessage |
* | text of errror message added to jrResult on error |
req |
* | express request |
jrResult |
* | our JrResult class with info about success or errors; pass in a valid instance and it is updated |
(inner) calcExpressMiddleWare(app)
- Source:
- See:
-
- github code
- github code ##### Notes * Uses an interesting trick to get file and line number where the middleware is located
Helper debug function that generates a list (object) of express middleware for display
Parameters:
Name | Type | Description |
---|---|---|
app |
object | the express app object |
Returns:
object containing all middleware, suitable for json stringifcation to console, etc.
(inner) calcExpressMiddlewareGetFileLine(handler)
- Source:
Helper funciton to try to get function file line from handle
Parameters:
Name | Type | Description |
---|---|---|
handler |
* |
Returns:
string specifying the line number of the handler
(inner) calcExpressMiddlewareRouterHint(middlewareHandle)
- Source:
Get a hint for a router middleware?
Parameters:
Name | Type | Description |
---|---|---|
middlewareHandle |
* |
(inner) calcExpressRoutePathData(expressApp)
- Source:
Helper debug function the generates a list (object) of all paths registered to the express app
Parameters:
Name | Type | Description |
---|---|---|
expressApp |
object |
Returns:
object containing all routes (and whether they are get|post|all)
(private, inner) calcExpressRoutePathDataRouteArray(routes, routepath, layer)
- Source:
Internal helper debug function the helps to generate a list (object) of all paths registered to the express app
Parameters:
Name | Type | Description |
---|---|---|
routes |
* | |
routepath |
* | |
layer |
* |
(private, inner) calcExpressRoutePathDataSplit(thing)
- Source:
- See:
Internal helper debug function the helps to generate a list (object) of all paths registered to the express app. Splits on path separator.
Parameters:
Name | Type | Description |
---|---|---|
thing |
* |
Returns:
split of path
(inner) forgetSessionVar(req, varName)
- Source:
Deletes the varname from session data
Parameters:
Name | Type | Description |
---|---|---|
req |
obj | |
varName |
string |
(inner) normalizePort(portval)
- Source:
Suggested code from express builder sample applications; used when creating server and binding it to ip+port
Parameters:
Name | Type | Description |
---|---|---|
portval |
* |
Returns:
port value (possibly provided as string), converted to an integer if possible
(inner) parseReqGetJsonField(req, keyName, jrResult)
- Source:
We expect a json encodd string in the request body under a certain field (post var) name. Get it and parse it to json and return the json Caller should check jrResult for any error
Parameters:
Name | Type | Description |
---|---|---|
req |
* | express request object |
keyName |
* | name of the post let to get the data from |
jrResult |
* | errors are pushed into here. |
Returns:
the json object encoded by the post var
(inner) reqOriginalUrl(req)
Parameters:
Name | Type | Description |
---|---|---|
req |
* |
Returns:
the original url of the request
(inner) reqPrefixedCheckboxItemIds(req, prefix)
- Source:
Express helper to look for all items with a given prefix
Parameters:
Name | Type | Description |
---|---|---|
req |
* | express request object |
prefix |
string | the prefix string |
Returns:
an array of all form values with id prefix_ (used for checklists)
(inner) reqPrefixedValueArray(req, prefix, keyList)
- Source:
Express helper to look for all properties with a given prefix
Parameters:
Name | Type | Description |
---|---|---|
req |
* | express request object |
prefix |
string | the prefix string |
keyList |
* |
Returns:
an associative array of all key=>value pairs of request properties where key starts with "prefix_key" and key is in keyList
(inner) reqUrlWithPath(req)
Parameters:
Name | Type | Description |
---|---|---|
req |
* |
Returns:
the url of the request
(inner) reqVal(req, key, defaultVal)
- Source:
Express helper function to look for property in request BODY or QUERY and return it, or defaultVal if not present
Parameters:
Name | Type | Description |
---|---|---|
req |
object | express request object |
key |
string | key to look for in request body or query |
defaultVal |
* | to return if key not found in request |
Returns:
req.body[key] or req.query[key] or defaultVal
(inner) reqValAsInt(req, key, min, max, defaultVal)
- Source:
Express helper to look for property in request BODY or QUERY and return it, or defaultVal if not present
Parameters:
Name | Type | Description |
---|---|---|
req |
object | express request object |
key |
string | key to look for in request body or query |
min |
int | min int to floor the value to |
max |
int | max int to ceil the value to |
defaultVal |
int | to return if key not found in request |
Returns:
req.body[key] or req.query[key] cast to int in range [min,max] or defaultVal
(inner) reqValFromList(req, key, valueList, defaultVal)
- Source:
Express helper to look for property in request BODY or QUERY and return it, or defaultVal if not present
Parameters:
Name | Type | Description |
---|---|---|
req |
object | express request object |
key |
string | key to look for in request body or query |
valueList |
array | list of values that are acceptable |
defaultVal |
* | to return if key not found in request |
Returns:
req.body[key] or req.query[key] as long as they are present in valueList, otherwise defaultVal
(inner) sendJsonDataSuccess(res, status, str, obj)
- Source:
Send a json reply to the express Response object
Parameters:
Name | Type | Description |
---|---|---|
res |
* | the express response object |
status |
int | status code to send |
str |
string | success string (send as value of success key) |
obj |
* | other data to send in reply |
(inner) sendJsonErorrAcl(jrContext, permission, permissionObjType, permissionObjId)
- Source:
Send a json result that indicates an ACL error, with status 403
Parameters:
Name | Type | Description |
---|---|---|
jrContext |
* | |
permission |
string | permission that the user is denied |
permissionObjType |
string | type of permission to complain about |
permissionObjId |
string | actual object id to complain about |
(inner) sendJsonError(res, status, errorStr, errorType)
- Source:
Send a json reply to the response, which is an error
Parameters:
Name | Type | Description |
---|---|---|
res |
* | the express response object |
status |
int | the http status code (error) |
errorStr |
string | the value for the error key |
errorType |
string | the value for the error key (OPTIONAL) |
(inner) sendJsonErrorAuthToken(jrContext)
- Source:
Send a json result that indicates an api token error, with status 403
Parameters:
Name | Type | Description |
---|---|---|
jrContext |
* | with the details of the error |
(inner) sendJsonResult(res, status, jrResult)
- Source:
Send a JrResult as a reply, either as an error or success, depending on jrResult data
Parameters:
Name | Type | Description |
---|---|---|
res |
* | the express repsonse object |
status |
int | the http status code |
jrResult |
* | the jrResult object to send |