Boilerplate middleware for Node projects in MyCareersFuture.
npm install @mcf/server-boilerplate-middleware
/healthz/healthz/readyz/readyz/metrics/metricsnpm or yarn:``sh`
npm i @mcf/server-boilerplate-middlewareor
yarn add @mcf/server-boilerplate-middleware
`js`
import {createServer} from '@mcf/server-boilerplate-middleware';
const server = createServer();
// ...
`js`
import {createServer} from '@mcf/server-boilerplate-middleware';
const {server} = createServer({
...options,
});
#### enableCookieParser : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableCookieParser: true}) |
#### enableCompression : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableCompression: true}) |
#### enableCSP : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableCSP: true}) |
#### enableCORS : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableCORS: true}) |
#### enableHttpHeadersSecurity : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableHttpHeadersSecurity: true}) |
#### enableMetrics : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableMetrics: true}) |
#### enableSerializer : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableSerializer: true}) |
#### enableServerLogging : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableServerLogging: true}) |
#### enableXray : BooleanBoolean
| Type | Default | Example |
| --- | --- | --- |
| | true | serverBoilerplate({enableXray: true}) |
#### compressionOptions : ObjectenableCompression
> This configuration is only relevant if the parameter was not set to false
| Key | Type | Notes | Defaults To |
| --- | --- | --- | --- |
| chunkSize | Number | size in bytes of chunk | 16384 |level
| | Number | 0-9 - see https://www.npmjs.com/package/compression for more information | 9 |threshold
| | Number | minimum size in bytes before compression kicks in | 102400 |
Defaults to:
`js`
const conpressionOptions = {
chunkSize: 16 * 1024, // 16kb
level: 9,
threshold: 300 * 1024, // 300kb
}
#### cspOptions : ObjectenableCSP
> This option is only relevant if the flag is not set to false.
| Key | Type | Notes | Defaults To |
| --- | --- | --- | --- |
| baseUri | Array | populates the base-uri value of the CSP header | ['\'none\''] |childSrc
| | Array | populates the child-src value of the CSP header | ['\'none\''] |connectSrc
| | Array | populates the connect-src value of the CSP header | ['\'none\''] |defaultSrc
| | Array | populates the default-src value of the CSP header | ['\'none\''] |formAction
| | Array | populates the form-action value of the CSP header | ['\'none\''] |fontSrc
| | Array | populates the font-src value of the CSP header | ['\'none\''] |frameAncestors
| | Array | populates the frame-ancestors value of the CSP header | ['\'none\''] |imgSrc
| | Array | populates the img-src value of the CSP header | ['\'none\''] |reportUri
| | String | populates the report-uri value of the CSP header | '/csp-report' |scriptSrc
| | Array | populates the script-src value of the CSP header | ['\'none\''] |styleSrc
| | Array | populates the style-src value of the CSP header | ['\'none\''] |upgradeInsecureRequests
| | Boolean | sets upgrade-insecure-requests value of the CSP header | unset |
Defaults to:
`js`
const cspOptions = {
baseUri: ['\'none\''],
childSrc: ['\'none\''],
connectSrc: ['\'none\''],
defaultSrc: ['\'none\''],
formAction: ['\'none\''],
fontSrc: ['\'none\''],
frameAncestors: ['\'none\''],
imgSrc: ['\'none\''],
reportUri: '/csp-report',
scriptSrc: ['\'none\''],
styleSrc: ['\'none\''],
upgradeInsecureRequests: false
}
The above configuration produces the following CSP:
``
"base-uri: 'none'; child-src 'none'; connect-src 'none'; default-src 'none'; form-action: 'none'; font-src 'none'; img-src 'none'; script-src 'none'; style-src 'none'; frame-ancestors 'none'; report-uri /csp-report"
#### corsOptions : ObjectenableCORS
> This configuration is only relevant if the parameter was not set to false
| Key | Type | Notes | Defaults To |
| --- | --- | --- | --- |
| allowedHeaders | Array | provides the Access-Control-Allow-Headers header value | [] |allowedMethods
| | Array | provides the Access-Control-Allow-Methods header value | ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'] |allowedOrigins
| | Array | provides the Access-Control-Allow-Origins header value | [] |credentials
| | Boolean | provides the Access-Control-Allow-Credentials header value | true |preflightContinue
| | Boolean | decides whether to pass the request on or respond with 204 | false |
Defaults to:
`js`
const corsOptions = {
allowedHeaders: [],
allowedMethods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
allowedOrigins: [],
credentials: true,
preflightContinue: false,
}
#### metricsOptions : ObjectenableMetrics
> This configuration is only relevant if the parameter was not set to false
| Key | Type | Notes | Defaults To |
| --- | --- | --- | --- |
| livenessCheckEndpoint | String | defines the liveness check endpoint for ignoring in metrics | '/healthz' |metricsEndpoint
| | String | defines the metrics endpoint for ignoring in metrics | '/metrics' |probeIntervalInMilliseconds
| | Number | defines interval between metrics scrape | 3000 |readinessCheckEndpoint
| | String | defines the readiness check endpoint for ignoring in metrics | '/readyz' |pushgatewayUrl
| | String | defines the pushgateway URL - when this is not null, the pushgateway is considered activated | null |pushgatewayJobName
| | String | defines the job name of the job being pushed to the pushgateway - use this to define the application instance when running in a cluster | process.env.USER || 'unknown' |pushgatewayTimeout
| | Number | defines the timeout of the pushgateway if enabled | 10000 |
Defaults to:
`js`
const metricsOptions = {
livenessCheckEndpoint: '/healthz',
metricsEndpoint: '/metrics',
probeIntervalInMilliseconds: 3000,
readinessCheckEndpoint: '/readyz',
pushgatewayUrl: null,
pushgatewayJobName: process.env.USER || 'unknown',
pushgatewayTimeout: 10000,
}
#### loggingOptions : ObjectenableServerLogging
> This configuration is only relevant if the parameter was not set to false
| Key | Type | Notes | Defaults To |
| --- | --- | --- | --- |
| additionalTokenizers | Array of Tokenizers | Additional tokenizers with the schema {id: string, fn: (req: Request, res: Response) => any} | [] |logger
| | IApplicationLogger | Used by the server to create a child logger | undefined |logStream
| | String | Specifies a stream to use instead of the default console. For example, use this to link Morgan up with Winston | null |hostnameType
| | String | If set to "os", the os.hostname() will be used. For all other values, process.env[hostnameType] is used. | "os"
Defaults to:
`js`
const loggingOptions = {
additionalTokenizers: [],
logger: createLogger(),
logStream: null,
hostnameType: 'os',
}
`sh`
npx lerna bootstrap
and running the tests from within the node container.To run the tests during development, use at the root directory:
`sh`
npx lerna run --scope @mcf/server-boilerplate-middleware test:watch
To run the tests on the built package, use:
`sh`
npx lerna run --scope @mcf/server-boilerplate-middleware test
To run a test server using the boilerplate server, use:
`sh`
npx lerna run --scope @mcf/server-boilerplate-middleware start
sh
npx lerna run --scope @mcf/server-boilerplate-middleware build
`$3
Run the following to setup an example environment:
Open a new terminal and run the following to create server a on port 11111:
`bash
SVC_ID=a PORT=11111 npm start;
`Open another terminal and run the following to create server b on port 22222:
`bash
RSVC_ID=a SVC_ID=b PORT=22222 PROXY_PORT=11111 npm start;
`Verify that your local Zipkin instance works and then run the following in yet another terminal to demonstrate tracing:
`bash
curl "http://localhost:22222/proxy";
`ChangeLog
$3
- set frameguard action (i.e. x-frame-options) to deny
- set frameAncestors csp directive to none
$3
- set referrer policy to strict-origin-when-cross-origin explicitly
$3
- update 'strict-transport-security : max-age to the recommended period of 2 years
$3
- update 'strict-transport-security : max-age=31536000; includeSubDomains; preload' as per CSA requirements
#### 0.8.5
- removed zipkin
- added aws xray tracing#### 0.8.2-4
- added keepalive and header timeout configuration
#### 0.8.1
- changed configuration signature for tracing
$3
#### 0.7.0
- added distributed tracing capabilities
- server instance now exports the following methods:
- .getTracer()
- .getContext()
- .getRequest()$3
#### 0.6.4
- added :logStream property in loggingOptions options for providing Morgan with a custom logger to use
#### 0.6.0
- added Morgan server request logging
$3
#### 0.5.3
- changed the preflightContinue option to be false by default
#### 0.5.1
- added features to accommodate a push gateway model (see pushgatewayUrl, pushgatewayTimeout and pushgatewayJobName for more info)
- if pushgatewayUrl is defined in the metricsOptions options property, the push gateway metrics flow model is activated, metrics will be pushed every :probeIntervalInMilliseconds millisecondsenableMetrics and metricsOptions properties)enableCORS and corsOptions)enableCSP, for server initialisationconnect-src to CSP configurationContent-Type: application/jsonContent-Type: application/x-www-form-urlencodedrequire('@mcf/server-boilerplate-middleware') without a .default property#### 0.0.2
- initial commit with an Express compatible server