1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
===================================================================
Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END============================================ */
var express = require('express');
var app = express();
var https = require('https');
var fs = require('fs');
const options = {
key: fs.readFileSync(process.env.HTTPS_KEY_FILE),
cert: fs.readFileSync(process.env.HTTPS_CERT_FILE)
};
var bodyParser = require('body-parser');
var textParser = bodyParser.text({ type: 'text/*' });
const path = require('path');
const PORT = process.env.PORT || 8080;
const CDT_HOME= process.env.CDT_HOME;
const LOG_DIR= process.env.LOG_DIR;
const MaxLogSize= process.env.MaxLogSize || 3000000;
//const DIST_FOLDER = path.join(process.cwd(), 'dist');
//const DIST_FOLDER = path.join( CDT_HOME, 'dist');
const LogF = path.join( LOG_DIR, 'ndserver.log' );
var chproc = require("child_process");
//var serverDataSvc = require('./dist/server-data-svc');
var serverDataSvc = require('./server-data-svc');
var logger = require('./srvlogger');
app.get('/getUserRole*', (req, res) => {
console.log(`get: Start: /getUserRole/* route...`);
logger.addLog("get: Start: /getUserRole/* route...\n",LogF);
if( CDT_HOME != null ) {
logger.addLog("get: CDT_HOME:["+CDT_HOME+"]\n",LogF);
};
var respData= serverDataSvc.getUserRoles( req.url );
logger.addLog("get: /getUserRole/*: typeof response:["+
(typeof respData)+"]\n",LogF);
logger.addLog("get: /getUserRole/*: stringify respData:["+
JSON.stringify(respData).substr(0,300)+"...]\n",LogF);
if( respData.length != null && respData.length != undefined ) {
logger.addLog("get: /getUserRole/*: respData.length="+
respData.length+"\n",LogF);
} else {
logger.addLog("get: /getUserRole/*: respData.length not defined !\n",LogF);
};
res.status(200).send( respData );
let timeStamp = new Date().toISOString();
logger.addLog("get: done: timeStamp:["+timeStamp+"]\n",LogF);
});
app.get('/api/*', (req, res) => {
console.log(`get: Start: /api/* route...`);
logger.addLog("get: Start: /api/* route...\n",LogF);
if( req.url == null ) {
console.log("get: /api/*: req.url is null !");
logger.addLog("get: /api/*: req.url is null !\n",LogF);
}
else {
// console.log("get: /api/*: req.url:["+ req.url+"]");
logger.addLog("get: /api/*: req.url:["+ req.url+"]\n",LogF);
};
if( req.hostname == null ) {
console.log("get: /api/*: req.hostname is null !");
logger.addLog("get: /api/*: req.hostname is null !\n",LogF);
}
else {
// console.log("get: /api/*: req.hostname:["+ req.hostname+"]");
logger.addLog("get: /api/*: req.hostname:["+ req.hostname+"]\n",LogF);
};
if( req.params != null ) {
// console.log("get: /api/*: Have req.params ...");
logger.addLog("get: /api/*: Have req.params ...\n",LogF);
if( req.params.length != null ) {
// console.log("get: /api/*: req.params.length="+req.params.length );
logger.addLog("get: /api/*: req.params.length="+req.params.length+"\n",LogF);
}
};
if( req.socket != null ) {
// console.log("get: /api/*: Have req.socket ...");
logger.addLog("get: /api/*: Have req.socket ...\n",LogF);
if( req.socket.remoteAddress != null ) {
// console.log("get: /api/*: remoteAddress:["+ req.socket.remoteAddress+"]");
logger.addLog(
"get: /api/*: remoteAddress:["+ req.socket.remoteAddress+"]\n",LogF);
}
else {
console.log("get: /api/*: req.socket.remoteAddress is null !");
logger.addLog("get: /api/*: req.socket.remoteAddress is null !\n",LogF);
}
};
if( CDT_HOME != null ) {
logger.addLog("get: CDT_HOME:["+CDT_HOME+"]\n",LogF);
};
var respData= serverDataSvc.getData( req.url );
logger.addLog("get: /api/*: typeof response:["+(typeof respData)+"]\n",LogF);
if( typeof respData == "string" ) {
logger.addLog("get: /api/*: respData:length="+respData.length+"\n",LogF);
};
logger.addLog("get: /api/*: stringify respData:["+
JSON.stringify(respData).substr(0,300)+"...]\n",LogF);
if( respData.length != null && respData.length != undefined ) {
logger.addLog("get: /api/*: respData.length="+respData.length+"\n",LogF);
} else {
logger.addLog("get: /api/*: respData.length not defined !\n",LogF);
};
res.status(200).send( respData );
let timeStamp = new Date().toISOString();
logger.addLog("get: /api/* done: timeStamp:["+timeStamp+"]\n\n",LogF);
});
app.post('/api/*', textParser, (req, res) => {
console.log(`post: Start: /api/* route...`);
logger.addLog("post: Start: /api/* route...\n",LogF);
let timeStamp = new Date().toISOString();
logger.addLog("post: /api/*: timeStamp:["+timeStamp+"]\n\n",LogF);
if( req.url == null ) {
console.log("post: /api/*: req.url is null !");
logger.addLog("post: /api/*: req.url is null !\n",LogF);
}
else {
console.log("post: /api/*: req.url:["+ req.url+"]");
logger.addLog("post: /api/*: req.url:["+ req.url+"]\n",LogF);
};
if( req.body == null ) {
console.log("post: /api/*: req.body is null !");
}
else {
console.log("post: /api/*: req.body.length="+req.body.length );
};
if( req.headers != null ) {
console.log("post: /api/*: req.headers.length="+ req.headers.length );
};
var remAddr= '';
if( req.socket != null ) {
console.log("post: /api/*: Have req.socket ...");
logger.addLog("post: /api/*: Have req.socket ...\n",LogF);
if( req.socket.remoteAddress != null ) {
logger.addLog(
"post: /api/*: remoteAddress:["+ req.socket.remoteAddress+"]\n",LogF);
remAddr= req.socket.remoteAddress;
}
else {
console.log("post: /api/*: req.socket.remoteAddress is null !");
logger.addLog("post: /api/*: req.socket.remoteAddress is null !\n",LogF);
}
};
logger.addLog(
"post: /api/*: req.params:["+JSON.stringify(req.params)+"]\n",LogF);
//.. checking url
var rexpLR= new RegExp(/\/api\/post_logrec/);
var matchLR= rexpLR.exec( req.url );
var rspData= 'r_undef';
if( matchLR ) {
//.. posting server-side log record
logger.addLog("post: /api/*: start postLog: remAddr:["+remAddr+"]\n",LogF);
rspData= serverDataSvc.postSrvLogRec( req.url, req.body, remAddr );
logger.addLog("post: /api/*: to send: response:["+rspData+"]\n",LogF);
}
else { //.. process data request
logger.addLog("post: /api/*: start procReq: remAddr:["+remAddr+"]\n",LogF);
rspData= serverDataSvc.procReq( req.url, req.body, remAddr );
logger.addLog("post: /api/*: to send: response:["+rspData+"]\n",LogF);
var rspObj= JSON.parse(rspData);
if( rspObj.respStr != null && rspObj.respStr != undefined ) {
var respStrLen= rspObj.respStr.length;
logger.addLog("post: /api/*: respStr length="+respStrLen+"\n",LogF);
if( respStrLen < 60 ) {
logger.addLog("post: /api/*: rspObj.respStr:["+rspObj.respStr+"]\n",LogF);
} else {
logger.addLog("post: /api/*: rspObj.respStr(part):["+
rspObj.respStr.substr(0,60)+"]\n",LogF);
}
}
}
res.status(200).send( rspData );
timeStamp = new Date().toISOString();
logger.addLog("post: done: timeStamp:["+timeStamp+"]\n\n",LogF);
});
//.. express.static to serve static files from /browser
//app.get('*.*', express.static(DIST_FOLDER) );
app.get('*.*', express.static(CDT_HOME) );
// All regular routes use the Universal engine
app.get('*', (req, res) => {
console.log(`get: Start: regular route...`);
logger.addLog("get: * Start: regular route...\n",LogF);
if( req.url == null ) {
console.log("get: req.url is null !");
logger.addLog("get: * regular route req.url is null !\n",LogF);
}
else {
console.log("get: req.url:["+ req.url+"]");
logger.addLog("get: * regular route: req.url:["+ req.url+"]\n",LogF);
};
if( CDT_HOME != null ) {
logger.addLog("get: * CDT_HOME:["+CDT_HOME+"]\n",LogF);
};
if( req.socket != null ) {
logger.addLog("get: * Have req.socket ...\n",LogF);
if( req.socket.remoteAddress != null ) {
logger.addLog(
"get: *: remoteAddress:["+ req.socket.remoteAddress+"]\n",LogF);
}
else {
logger.addLog("get: *: req.socket.remoteAddress is null !\n",LogF);
}
};
var respData= serverDataSvc.getData( req.url );
logger.addLog("get: *: typeof response:["+(typeof respData)+"]\n",LogF);
if( typeof respData == "string" ) {
logger.addLog("get: *: respData:length="+respData.length+"\n",LogF);
};
// logger.addLog("get: *: respData:["+JSON.stringify(respData)+"]\n",LogF);
res.status(200).send( respData );
let timeStamp = new Date().toISOString();
logger.addLog("get: * done: timeStamp:["+timeStamp+"]\n\n",LogF);
});
// Start up the Node server
https.createServer(options,app).listen(PORT, () => {
console.log(`Node server: CDT_HOME:[${CDT_HOME}]`);
console.log(`Node server: LOG_DIR:[${LOG_DIR}]`);
console.log(`Node server: opening Log in the file:[${LogF}]`);
console.log(`Node server listening on http://localhost:${PORT}`);
// console.log(" DIST_FOLDER:["+DIST_FOLDER+"]");
logger.setMaxLogSize( MaxLogSize );
logger.addLog("\n Node server: start: CDT_HOME:["+CDT_HOME+"]\n",LogF);
serverDataSvc.setHomeDir( CDT_HOME, LOG_DIR );
});
|