1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd: Quick and Easy Web Development |
|
5 | | |
|
6 | | Copyright (c) 2017-19 M/Gateway Developments Ltd, |
|
7 | | Redhill, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 27 March 2019
|
28 |
|
29 | Thanks to:
|
30 | Ward De Backer for:
|
31 | - body-parser enhancement
|
32 | - beforeRouter and afterRouter enhancement
|
33 |
|
34 | */
|
35 |
|
36 | var express = require('express');
|
37 | var bodyParser;
|
38 | var app = express();
|
39 |
|
40 | var qx;
|
41 | var q;
|
42 |
|
43 | function configure(config, routes, qOper8, qExt) {
|
44 | qx = qExt;
|
45 | q = qOper8;
|
46 |
|
47 | // if user instantiates his/her own bodyParser module, use it
|
48 | // Note: user must then also define the app.use express middleware to use it
|
49 | if (config.bodyParser) {
|
50 | console.log('Custom Body Parser has been loaded');
|
51 | bodyParser = config.bodyParser;
|
52 | }
|
53 | else {
|
54 | bodyParser = require('body-parser');
|
55 | app.use(bodyParser.json());
|
56 | // note: config.webServer will be 'express'
|
57 | }
|
58 |
|
59 | if (config.qewd_up) {
|
60 | // augment req with method to allow optional additional forwarding of messages
|
61 | // in onWSRequest handler - one-way fire and forget messages
|
62 | // eg for recording audit trails
|
63 | // Note: always a POST
|
64 |
|
65 | var sendFireAndForgetMsg = function(req, params) {
|
66 |
|
67 | var _req = {
|
68 | originalUrl: params.url,
|
69 | method: 'POST',
|
70 | headers: req.headers,
|
71 | body: params.body
|
72 | };
|
73 |
|
74 | // dummy res object to fool qx.handleMessage and bypass normal response
|
75 | // handling - we don't want the response to go back to client
|
76 |
|
77 | var _res = {
|
78 | set: function() {return false},
|
79 | status: function() {
|
80 | return this;
|
81 | },
|
82 | send: function(obj) {
|
83 | return false;
|
84 | },
|
85 | locals: {}
|
86 | };
|
87 | qx.handleMessage(_req, _res);
|
88 | };
|
89 |
|
90 | app.use(function(req, res, next) {
|
91 | req.sendFireAndForgetMsg = sendFireAndForgetMsg;
|
92 | return next();
|
93 | });
|
94 |
|
95 | }
|
96 |
|
97 | // if user defines addMiddleware method, give them complete control!
|
98 | if (config.addMiddleware) {
|
99 | config.addMiddleware(bodyParser, app, q, qx, config);
|
100 | }
|
101 | else if (config.addMiddlewareUp) {
|
102 | config.addMiddlewareUp.call(q, bodyParser, app, qx.router, config, qx);
|
103 | }
|
104 |
|
105 | app.post('/ajax', function(req, res) {
|
106 | console.log('/ajax body: ' + JSON.stringify(req.body));
|
107 | req.headers.qewd = 'ajax';
|
108 | qx.handleMessage(req, res);
|
109 | });
|
110 |
|
111 | console.log('webServerRootPath = ' + config.webServerRootPath);
|
112 | if (config.cors) {
|
113 | app.use('/', function (req, res, next) {
|
114 | //res.header('Access-Control-Allow-Origin', '*');
|
115 | //res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
116 |
|
117 | res.header('Access-Control-Allow-Credentials', 'true');
|
118 | res.header('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization');
|
119 | res.header('Access-Control-Allow-Methods', 'GET, PUT, DELETE, POST, OPTIONS');
|
120 | res.header('Access-Control-Allow-Origin', '*');
|
121 | next();
|
122 | });
|
123 | }
|
124 | app.use('/', express.static(config.webServerRootPath))
|
125 |
|
126 | if (routes) {
|
127 | routes.forEach(function(route) {
|
128 |
|
129 | // Ward DeBacker: September 2017
|
130 | // create (variable) arguments array to pass to app.use()
|
131 |
|
132 | var args = [route.path];
|
133 | // add array with custom middleware to add before qx.router is called (if present)
|
134 |
|
135 | if (route.beforeRouter && Array.isArray(route.beforeRouter)) args = args.concat(route.beforeRouter);
|
136 |
|
137 | // in case route.afterRouter is present, nextCallback must be set to true
|
138 | // to prevent handleMessage() sending reponse. User will have done this
|
139 |
|
140 | var options = {
|
141 | nextCallback: route.afterRouter ? true : false
|
142 | };
|
143 | args = args.concat([qx.router(options)]);
|
144 |
|
145 | // add array with custom middleware to add after qx.router is called (if present)
|
146 |
|
147 | if (route.afterRouter && Array.isArray(route.afterRouter)) args = args.concat(route.afterRouter);
|
148 |
|
149 | // define the Express route by calling app.use()
|
150 |
|
151 | app.use.apply(app, args); // allow user to define custom middleware for each route
|
152 | //app.use(path, qx.router());
|
153 | console.log('route ' + route.path + ' will be handled by qx.router');
|
154 | });
|
155 | }
|
156 | return app;
|
157 | }
|
158 |
|
159 | module.exports = configure;
|