11'use strict' ;
22
3- const _ = require ( 'underscore' ) ;
43const Express = require ( 'express' ) ;
54const BodyParser = require ( 'body-parser' ) ;
65
76module . 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} ;
0 commit comments