@apolitical/server 2.2.0 → 2.3.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 +8 -0
- package/package.json +1 -1
- package/src/config.js +6 -1
- package/src/container.js +2 -2
- package/src/loaders/fallback.loader.js +21 -0
- package/src/loaders/middlewares.loader.js +21 -5
- package/src/middlewares/error.middleware.js +10 -4
- package/src/services/server.service.js +4 -6
- package/src/loaders/static.loader.js +0 -17
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.1] - 2022-02-15
|
|
9
|
+
### Changed
|
|
10
|
+
- Body parser types
|
|
11
|
+
|
|
12
|
+
## [2.3.0] - 2022-02-14
|
|
13
|
+
### Added
|
|
14
|
+
- fallbackController
|
|
15
|
+
|
|
8
16
|
## [2.2.0] - 2022-02-07
|
|
9
17
|
### Changed
|
|
10
18
|
- Pipeline uses `Node-Publish` template
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -35,7 +35,12 @@ module.exports = {
|
|
|
35
35
|
credentials: true,
|
|
36
36
|
},
|
|
37
37
|
BODY_PARSER_OPTIONS: {
|
|
38
|
-
|
|
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
|
@@ -36,7 +36,7 @@ const documentationLoader = require('./loaders/documentation.loader');
|
|
|
36
36
|
const loggerLoader = require('./loaders/logger.loader');
|
|
37
37
|
const middlewaresLoader = require('./loaders/middlewares.loader');
|
|
38
38
|
const probesLoader = require('./loaders/probes.loader');
|
|
39
|
-
const
|
|
39
|
+
const fallbackLoader = require('./loaders/fallback.loader');
|
|
40
40
|
// Middleware
|
|
41
41
|
const authenticationMiddleware = require('./middlewares/authentication.middleware');
|
|
42
42
|
const authorisationMiddleware = require('./middlewares/authorisation.middleware');
|
|
@@ -85,7 +85,7 @@ container.register({
|
|
|
85
85
|
loggerLoader: asFunction(loggerLoader).singleton(),
|
|
86
86
|
middlewaresLoader: asFunction(middlewaresLoader).singleton(),
|
|
87
87
|
probesLoader: asFunction(probesLoader).singleton(),
|
|
88
|
-
|
|
88
|
+
fallbackLoader: asFunction(fallbackLoader).singleton(),
|
|
89
89
|
// Middlewares
|
|
90
90
|
authenticationMiddleware: asFunction(authenticationMiddleware).singleton(),
|
|
91
91
|
authorisationMiddleware: asFunction(authorisationMiddleware).singleton(),
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = ({ logger }) => {
|
|
4
|
+
return function load(app, { fallbackController, staticFiles }) {
|
|
5
|
+
const childLogger = logger.where(__filename, 'load');
|
|
6
|
+
childLogger.debug('Started');
|
|
7
|
+
|
|
8
|
+
if (staticFiles) {
|
|
9
|
+
const { baseUrl, indexFilePath } = staticFiles;
|
|
10
|
+
// Load fallback or static content
|
|
11
|
+
if (fallbackController) {
|
|
12
|
+
app.get(`${baseUrl}*`, fallbackController);
|
|
13
|
+
} else if (indexFilePath) {
|
|
14
|
+
app.get(`${baseUrl}*`, function (req, res) {
|
|
15
|
+
res.sendFile(indexFilePath);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
childLogger.debug('Finished');
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -1,20 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
module.exports = ({
|
|
4
|
-
|
|
3
|
+
module.exports = ({
|
|
4
|
+
bodyParser: { json, urlencoded },
|
|
5
|
+
express,
|
|
6
|
+
compression,
|
|
7
|
+
cors,
|
|
8
|
+
cookieParser,
|
|
9
|
+
prerender,
|
|
10
|
+
config,
|
|
11
|
+
logger,
|
|
12
|
+
}) => {
|
|
13
|
+
const {
|
|
14
|
+
BODY_PARSER_OPTIONS: { JSON_OPTIONS, URL_ENCODED_OPTIONS },
|
|
15
|
+
CORS_OPTIONS,
|
|
16
|
+
} = config.SERVER;
|
|
5
17
|
|
|
6
|
-
return function load(app, { corsOptions, prerenderToken }) {
|
|
18
|
+
return function load(app, { corsOptions, prerenderToken, staticFiles }) {
|
|
7
19
|
const childLogger = logger.where(__filename, 'load');
|
|
8
20
|
childLogger.debug('Started');
|
|
9
21
|
// Load useful middlewares
|
|
10
22
|
app.use(cookieParser());
|
|
11
|
-
app.use(json());
|
|
12
|
-
app.use(urlencoded(
|
|
23
|
+
app.use(json(JSON_OPTIONS));
|
|
24
|
+
app.use(urlencoded(URL_ENCODED_OPTIONS));
|
|
13
25
|
app.use(cors(Object.assign({}, CORS_OPTIONS, corsOptions)));
|
|
14
26
|
app.use(compression());
|
|
15
27
|
if (prerenderToken) {
|
|
16
28
|
app.use(prerender.set('prerenderToken', prerenderToken));
|
|
17
29
|
}
|
|
30
|
+
if (staticFiles) {
|
|
31
|
+
const { baseUrl, folderPath } = staticFiles;
|
|
32
|
+
app.use(baseUrl, express.static(folderPath));
|
|
33
|
+
}
|
|
18
34
|
childLogger.debug('Finished');
|
|
19
35
|
};
|
|
20
36
|
};
|
|
@@ -9,7 +9,7 @@ module.exports =
|
|
|
9
9
|
logger,
|
|
10
10
|
serverError: { GeneralError },
|
|
11
11
|
}) =>
|
|
12
|
-
({ labels, redirectURL }) => {
|
|
12
|
+
({ labels, redirectURL, fallbackController }) => {
|
|
13
13
|
// Apolitical Logger (with service metadata)
|
|
14
14
|
const errorLogger = logger.where(__filename, 'errorMiddleware', labels);
|
|
15
15
|
|
|
@@ -43,10 +43,16 @@ module.exports =
|
|
|
43
43
|
} else {
|
|
44
44
|
errorLogger.warn(message, { errors });
|
|
45
45
|
}
|
|
46
|
+
|
|
46
47
|
if (redirectURL) {
|
|
47
|
-
res.redirect(TEMPORARY_REDIRECT, `${redirectURL}?errorMessage=${encodeURI(message)}`);
|
|
48
|
-
}
|
|
49
|
-
|
|
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
|
+
}
|
|
50
55
|
}
|
|
56
|
+
return res.status(code).json({ message, errors });
|
|
51
57
|
};
|
|
52
58
|
};
|
|
@@ -11,11 +11,11 @@ module.exports = ({
|
|
|
11
11
|
errorMiddleware,
|
|
12
12
|
middlewaresLoader,
|
|
13
13
|
probesLoader,
|
|
14
|
-
|
|
14
|
+
fallbackLoader,
|
|
15
15
|
jwtService,
|
|
16
16
|
serverError,
|
|
17
17
|
}) => {
|
|
18
|
-
let terminator = null;
|
|
18
|
+
let terminator = null; // I need your clothes, your boots and your motorcycle.
|
|
19
19
|
|
|
20
20
|
function run(app, port) {
|
|
21
21
|
const childLogger = logger.where(__filename, 'run');
|
|
@@ -52,10 +52,8 @@ module.exports = ({
|
|
|
52
52
|
}
|
|
53
53
|
// Load documentation
|
|
54
54
|
documentationLoader(app, opts);
|
|
55
|
-
// Load
|
|
56
|
-
|
|
57
|
-
staticLoader(app, opts);
|
|
58
|
-
}
|
|
55
|
+
// Load fallback routing
|
|
56
|
+
fallbackLoader(app, opts);
|
|
59
57
|
// Use error handler
|
|
60
58
|
if (opts.handleErrors) {
|
|
61
59
|
app.use(errorMiddleware(opts));
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = ({ express, logger }) => {
|
|
4
|
-
return function load(app, { staticFiles: { baseUrl, folderPath, indexFilePath } }) {
|
|
5
|
-
const childLogger = logger.where(__filename, 'load');
|
|
6
|
-
childLogger.debug('Started');
|
|
7
|
-
// Use Express built-in middleware
|
|
8
|
-
app.use(baseUrl, express.static(folderPath));
|
|
9
|
-
// Load static endpoint
|
|
10
|
-
if (indexFilePath) {
|
|
11
|
-
app.get(`${baseUrl}*`, function (req, res) {
|
|
12
|
-
res.sendFile(indexFilePath);
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
childLogger.debug('Finished');
|
|
16
|
-
};
|
|
17
|
-
};
|