server
import { server } from '@dr.pogodin/react-utils';
The server module provides constants and methods facilitating implementation, setup and launch of a configurable ReactJS web server.
Beside holding attached constants and functions documented below, server
export itself is also a function, which creates and launches a web-server for
ReactJS application. See server() documentation for further details.
Constants
- errors.CODES - A map from HTTP status code names to the corresponding numeric codes.
- errors.ERRORS - A map from HTTP status code names to their pretty-printed names.
- SCRIPT_LOCATIONS - The map of valid
location
values in Script objects.
Functions
- errors.assert() - Validates a value against the given Joi schema.
- errors.fail() - Throws an error with the given message and HTTP status code.
- errors.getErrorForCode() - Given HTTP status code returns the corresponding error text.
- errors.joi - An alias for Joi library, which provides methods for HTTP request validation.
- errors.newError() - Creates a new
Error
object with the given error message and attached HTTP status. - getDefaultCspSettings() - Returns a deep copy of default CSP settings object used by react-utils.
Constants
errors.CODES
server.errors.CODES: object
A map from HTTP status code names to the corresponding numeric codes. It is an alias for StatusCodes object from http-status-codes library.
Example
import { server } from '@dr.pogodin/react-utils';
const { CODES } = server.errors;
console.log(CODES.BAD_REQUEST);
// Prints: 400
errors.ERRORS
server.errors.ERRORS: object
A map from HTTP status code names to their pretty-printed names. It is an alias for ReasonPhrases object from http-status-codes library.
Example
import { server } from '@dr.pogodin/react-utils';
const { ERRORS } = server.errors;
console.log(ERRORS.BAD_REQUEST);
// Prints: Bad Request
SCRIPT_LOCATIONS
server.SCRIPT_LOCATIONS: object
This object provides the map of valid Script's location
values:
BODY_OPEN
- Right after the opening<body>
tag.DEFAULT
- In the end of<body>
tag, just before the main application bundle. This is the default location where scripts are injected when provided as strings rather than Script objects.HEAD_OPEN
- Right after the opening<head>
tag.
Functions
errors.assert()
server.errors.assert(value, schema, message = '', statusCode = 400)
Validates value
against the given Joi schema
. Throws an error with
the given error message
and statusCode
if the validation fails.
Arguments
value
- any value to validate.schema
- object - Joi schema.message = ''
- string - Optional. Error message. If given, the error message from Joi will be appended to it, otherwise the error message from Joi is used as is.statusCode = 400
- number - Optional. Error status code. Defaults400
(bad request).
errors.fail()
server.errors.fail(message, statusCode = 500)
Throws an error with the given message
and HTTP statusCode
.
Arguments
message
- string - Error message.statusCode = 500
- number - Optional. HTTP status code. Defaults500
(internal server error).
errors.getErrorForCode()
server.errors.getErrorForCode(code): string
Given HTTP status code
returns the corresponding error text. It is an alias
for getReasonPhrase() function from http-status-codes library.
Arguments & Result
code
- number - HTTP status code.- Returns string - Error message.
errors.joi
server.errors.joi: object
An alias for Joi library, which provides methods useful for HTTP request validation.
Example: Joi methods are used to create validation schema for the body of a sample HTTP request.
import { server } from '@dr.pogodin/react-utils';
const { joi } = server.errors;
const requestBodySchema = joi.object({
sampleKey: joi.string().max(16).required(),
});
errors.newError()
server.errors.newError(message, statusCode = 500): Error
Creates a new Error
object with given message, and HTTP statusCode
attached
to the result
as result.status
field.
Arguments & Result
message
- string - Error message.statusCode = 500
- number - Optional. HTTP status code. Defaults500
(Internal Server Error).- Returns Error.
getDefaultCspSettings()
server.getDefaultCspSettings(): object
Returns a deep copy of default CSP settings object used by react-utils,
with exception of nonce-xxx
clause in script-src
directive, which is added
dynamically for each request.