Skip to content

Commit 0bb1cac

Browse files
authored
Merge pull request #5 from BoolJS/develop
Refactored to decrease code complexity.
2 parents 7cb8e2d + faba277 commit 0bb1cac

6 files changed

Lines changed: 73 additions & 62 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
2-
before_install:
3-
- npm install booljs@latest
2+
before_script:
3+
- npm install booljs@latest --no-save
44
node_js:
55
- lts/*

example

Submodule example updated 1 file

lib/server/init.js

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,16 @@
11
'use strict';
22

3-
const _ = require('underscore');
43
const Express = require('express');
54
const BodyParser = require('body-parser');
65

76
module.exports = async function (instance) {
8-
let expressApplication = new Express();
7+
const application = new Express();
98

10-
// X-Powered-By: bool.js
11-
expressApplication.use(function (req, res, next) {
12-
res.header('X-Powered-By', 'booljs');
13-
next();
14-
});
15-
16-
var configuration = instance.getComponents().configuration.get('server');
17-
18-
// Sets listening hostname
19-
expressApplication.set('host', (
20-
process.env.IP || process.env.HOSTNAME || process.env.HOST ||
21-
(configuration && (
22-
configuration.ip || configuration.hostname || configuration.host
23-
)) ||
24-
'0.0.0.0'
25-
));
26-
27-
// Sets listening port
28-
expressApplication.set('port', (
29-
process.env.PORT || (configuration && configuration.port) || 3001
30-
));
31-
32-
// Enable body-parser middlewares
33-
var bodyParserOptions = (configuration && configuration.body) || undefined;
34-
35-
if (bodyParserOptions !== undefined) {
36-
expressApplication.use(BodyParser.urlencoded(
37-
_.extend({ extended: true }, bodyParserOptions))
38-
);
39-
expressApplication.use(BodyParser.json(bodyParserOptions));
40-
} else {
41-
expressApplication.use(BodyParser.urlencoded({ extended: true }));
42-
expressApplication.use(BodyParser.json());
43-
}
9+
exports.setPoweredBy(application);
10+
exports.configureServer(application, instance
11+
.getComponents().configuration.get('server'));
12+
exports.configureBodyParser(application, instance
13+
.getComponents().configuration.get('server'));
4414

4515
// Enables Json View
4616
if (!instance.getComponents().views) {
@@ -51,5 +21,44 @@ module.exports = async function (instance) {
5121
'Json', require('../views/json'), instance.getComponents().views
5222
);
5323

54-
return expressApplication;
24+
return application;
25+
};
26+
27+
/**
28+
* Renders the X-Powered-By header
29+
* @param {ExpressApplication} application - The express application
30+
*/
31+
exports.setPoweredBy = function (application) {
32+
application.use((request, response, next) => {
33+
response.header('X-Powered-By', 'booljs');
34+
next();
35+
});
36+
};
37+
38+
/**
39+
* Configures server options
40+
* @param {ExpressApplication} application - The express application
41+
* @param {Object} [configuration={}] - The configuration object
42+
*/
43+
exports.configureServer = function (application, configuration = {}) {
44+
let { IP, HOSTNAME, HOST, PORT } = process.env;
45+
let { ip, hostname, host, port } = configuration;
46+
// Sets listening hostname
47+
application.set('host',
48+
IP || HOSTNAME || HOST || ip || hostname || host || '0.0.0.0');
49+
50+
// Sets listening port
51+
application.set('port', PORT || port || 3001);
52+
};
53+
54+
/**
55+
* Enables body-parser middlewares
56+
* @param {ExpressApplication} application - The express application
57+
* @param {Object} [configuration={}] - The configuration object
58+
*/
59+
exports.configureBodyParser = function (application, configuration = {}) {
60+
let { body: options = {} } = configuration;
61+
62+
application.use(BodyParser.urlencoded({ extended: true, ...options }));
63+
application.use(BodyParser.json({ ...options }));
5564
};

lib/views/json.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
'use strict';
22

3-
const _ = require('underscore');
4-
5-
exports.execute = function (error, data, response, statusCode) {
6-
let status = error ? (error.status || 500) : (statusCode || 200);
7-
8-
let object = { success: !error };
9-
10-
if (error !== undefined && error !== null) {
11-
object.error = _.omit(error, 'status');
12-
object.error.message = error.message || 'internal_server_error';
13-
} else if (data !== undefined && data !== null) {
14-
object.data = data;
3+
exports.execute = function (error, data, response, status = 200) {
4+
if (error !== undefined && error !== null && error.status !== null) {
5+
status = error.status;
6+
delete error.status;
157
}
168

17-
response.status(status).json(object);
9+
response.status(status).json({
10+
success: error === undefined && error === null && !error,
11+
data,
12+
error
13+
});
1814
};
1915

2016
module.exports = class JsonView {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "booljs.express",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"description": "Express driver for bool.js",
55
"main": "index.js",
66
"keywords": [
@@ -32,7 +32,6 @@
3232
"underscore": "1.x"
3333
},
3434
"dependencies": {
35-
"async": "^2.2.0",
3635
"body-parser": "^1.18.2",
3736
"express": "^4.16.2"
3837
},
@@ -45,7 +44,8 @@
4544
"eslint-plugin-node": "^5.1.0",
4645
"eslint-plugin-promise": "^3.5.0",
4746
"eslint-plugin-standard": "^3.0.1",
48-
"mocha": "^4.0.1"
47+
"mocha": "^4.0.1",
48+
"supertest": "^3.0.0"
4949
},
5050
"engines": {
5151
"node": ">=8.0.0",

test/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
/* global describe, before, it */
21
'use strict';
32

43
const resolver = require('../lib/utils/resolve');
54
const Bool = require('booljs');
5+
const Agent = require('supertest');
66

77
describe('Bool.js', function () {
8-
let app;
8+
let server;
99

10-
before(() => {
11-
app = new Bool('com.example.api', [ resolver('') ])
10+
before(async () => {
11+
let app = await new Bool('com.example.api', [ resolver('') ])
1212
.setServerDrivers([ 'booljs.express' ])
13-
.setBase('example');
13+
.setBase('example')
14+
.run();
15+
server = new Agent(app.server);
1416
});
1517

16-
it('Boots using express.js', () => app.run());
18+
it('GET / -> 200 OK', () => server.get('/').expect(200));
19+
20+
it('GET /undefined -> 501 Not Implemented', () => server
21+
.get('/undefined')
22+
.expect(501));
1723
});

0 commit comments

Comments
 (0)