@apolitical/server 2.3.0-rc.fallback.4 → 2.3.2-rc.static.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.3.2] - 2022-02-21
9
+ ### Changed
10
+ - loading static files middleware after the app has been loaded
11
+
12
+ ## [2.3.1] - 2022-02-15
13
+ ### Changed
14
+ - Body parser types
15
+
8
16
  ## [2.3.0] - 2022-02-14
9
17
  ### Added
10
18
  - fallbackController
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.3.0-rc.fallback.4",
3
+ "version": "2.3.2-rc.static.1",
4
4
  "description": "Node.js module to encapsulate Apolitical's express server setup",
5
5
  "author": "Apolitical Group Limited <engineering@apolitical.co>",
6
6
  "license": "MIT",
package/src/config.js CHANGED
@@ -35,7 +35,12 @@ module.exports = {
35
35
  credentials: true,
36
36
  },
37
37
  BODY_PARSER_OPTIONS: {
38
- extended: false,
38
+ JSON_OPTIONS: {
39
+ type: ['application/json', 'application/csp-report', 'application/reports+json'],
40
+ },
41
+ URL_ENCODED_OPTIONS: {
42
+ extended: false,
43
+ },
39
44
  },
40
45
  MORGAN_OPTIONS: {
41
46
  LOGGED_OUT_SLUG: 'logged-out',
package/src/container.js CHANGED
@@ -35,6 +35,7 @@ const jwtPassportHelper = require('./helpers/jwt/passport.helper');
35
35
  const documentationLoader = require('./loaders/documentation.loader');
36
36
  const loggerLoader = require('./loaders/logger.loader');
37
37
  const middlewaresLoader = require('./loaders/middlewares.loader');
38
+ const staticLoader = require('./loaders/static.loader');
38
39
  const probesLoader = require('./loaders/probes.loader');
39
40
  const fallbackLoader = require('./loaders/fallback.loader');
40
41
  // Middleware
@@ -84,6 +85,7 @@ container.register({
84
85
  documentationLoader: asFunction(documentationLoader).singleton(),
85
86
  loggerLoader: asFunction(loggerLoader).singleton(),
86
87
  middlewaresLoader: asFunction(middlewaresLoader).singleton(),
88
+ staticLoader: asFunction(staticLoader).singleton(),
87
89
  probesLoader: asFunction(probesLoader).singleton(),
88
90
  fallbackLoader: asFunction(fallbackLoader).singleton(),
89
91
  // Middlewares
@@ -1,33 +1,23 @@
1
1
  'use strict';
2
2
 
3
- module.exports = ({
4
- bodyParser: { json, urlencoded },
5
- express,
6
- compression,
7
- cors,
8
- cookieParser,
9
- prerender,
10
- config,
11
- logger,
12
- }) => {
13
- const { BODY_PARSER_OPTIONS, CORS_OPTIONS } = config.SERVER;
3
+ module.exports = ({ bodyParser: { json, urlencoded }, compression, cors, cookieParser, prerender, config, logger }) => {
4
+ const {
5
+ BODY_PARSER_OPTIONS: { JSON_OPTIONS, URL_ENCODED_OPTIONS },
6
+ CORS_OPTIONS,
7
+ } = config.SERVER;
14
8
 
15
- return function load(app, { staticFiles, corsOptions, prerenderToken }) {
9
+ return function load(app, { corsOptions, prerenderToken }) {
16
10
  const childLogger = logger.where(__filename, 'load');
17
11
  childLogger.debug('Started');
18
12
  // Load useful middlewares
19
13
  app.use(cookieParser());
20
- app.use(json());
21
- app.use(urlencoded(BODY_PARSER_OPTIONS));
14
+ app.use(json(JSON_OPTIONS));
15
+ app.use(urlencoded(URL_ENCODED_OPTIONS));
22
16
  app.use(cors(Object.assign({}, CORS_OPTIONS, corsOptions)));
23
17
  app.use(compression());
24
18
  if (prerenderToken) {
25
19
  app.use(prerender.set('prerenderToken', prerenderToken));
26
20
  }
27
- if (staticFiles) {
28
- const { baseUrl, folderPath } = staticFiles;
29
- app.use(baseUrl, express.static(folderPath));
30
- }
31
21
  childLogger.debug('Finished');
32
22
  };
33
23
  };
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ express, logger }) => {
4
+ return function load(app, { staticFiles }) {
5
+ const childLogger = logger.where(__filename, 'load');
6
+ childLogger.debug('Started');
7
+
8
+ if (staticFiles) {
9
+ const { baseUrl, folderPath } = staticFiles;
10
+ app.use(baseUrl, express.static(folderPath));
11
+ }
12
+ childLogger.debug('Finished');
13
+ };
14
+ };
@@ -45,11 +45,14 @@ module.exports =
45
45
  }
46
46
 
47
47
  if (redirectURL) {
48
- res.redirect(TEMPORARY_REDIRECT, `${redirectURL}?errorMessage=${encodeURI(message)}`);
49
- } else if (fallbackController && message === 'Unexpected error: Cannot read items') {
50
- fallbackController(req, res);
51
- } else {
52
- res.status(code).json({ message, errors });
48
+ return res.redirect(TEMPORARY_REDIRECT, `${redirectURL}?errorMessage=${encodeURI(message)}`);
49
+ }
50
+ if (fallbackController) {
51
+ // more known error messages can be added here
52
+ if (message === 'Unexpected error: Cannot read items') {
53
+ return fallbackController(req, res);
54
+ }
53
55
  }
56
+ return res.status(code).json({ message, errors });
54
57
  };
55
58
  };
@@ -10,6 +10,7 @@ module.exports = ({
10
10
  authorisationMiddleware,
11
11
  errorMiddleware,
12
12
  middlewaresLoader,
13
+ staticLoader,
13
14
  probesLoader,
14
15
  fallbackLoader,
15
16
  jwtService,
@@ -50,12 +51,12 @@ module.exports = ({
50
51
  if (opts.appLoader) {
51
52
  await opts.appLoader(app);
52
53
  }
54
+ // Load static files middleware
55
+ staticLoader(app, opts);
53
56
  // Load documentation
54
57
  documentationLoader(app, opts);
55
-
56
- // Load static resources or fallback
58
+ // Load fallback routing
57
59
  fallbackLoader(app, opts);
58
-
59
60
  // Use error handler
60
61
  if (opts.handleErrors) {
61
62
  app.use(errorMiddleware(opts));