I'm a novice about nodejs, for this I need your help . I have to create a local proxy server on port 8000 that receives soap requests from a client application.
The proxy retrieves the namespace of soap and depending on its value requires or not to type a PIN through a gui to user.
At this point the proxy insert in the request header some information and the PIN.
Then sends POST requests to a web server through a corporate proxy and get the response. I have used the module 'request' to create the proxy and the PIN request to the user is made throwing an executable by calling require('child_process').execSync.
The executable writes the pin in a json file then recovered from the service. To retrieve the body and test the namespace I used events request.on('date', function () { ... }) and request.on('end', function () { ... }).
The problem is that, when the request is post to web server, I get the following error:
Error: ETIMEDOUT (code: ETIMEDOUT)
.\test.js:69
response.writeHead(error.code, {'content-type': 'text/xml'});
^
TypeError: Cannot read property 'writeHead' of undefined
at Request._callback (.\test2.js:69:16)
at self.callback (.\node_modules\request\request.js:344:22)
at Request.emit (events.js:129:20)
at null._onTimeout (.\node_modules\request\request.js:911:12)
at Timer.listOnTimeout (timers.js:110:15)
Some help or idea ? Below the code.
var http = require('http');
var fs = require("fs");
var _request = require('request');
var execSync = require('child_process').execSync;
var reqPinNamespace = 'myNamespace';
process.env.http_proxy = 'corporateProxyHttp';
process.env.https_proxy = 'corporateProxyHttps';
var opt = {
url: '',
strictSSL: true,
tunnel: true,
headers: {},
timeout: 10000,
followRedirect: true,
maxRedirects: 10
};
function proxyOpt(req)
{
var _headers = [];
for(var item in req.headers) {
if((req.headers[item] != undefined) && (item !='content-length'))
_headers.push(item + ": " + req.headers[item]);
}
_headers.push('mycred:' + 'ABCDEFG');
opt.url = 'http://myServerWeb';
opt.headers = _headers;
}
function requestPIN()
{
var child = execSync('GetPIN.exe');
var jdata = require('filePin.json');
fs.unlink('filePin.json');
opt.headers.push("pin: " + jdata.pin);
}
var proxyHTTP = http.createServer(function(request, response) {
proxyOpt(request);
var _body;
request.on('data', function(chunk) {
_body += chunk.toString();
});
request.on('end', function() {
if(_body.indexOf(reqPinNamespace) > -1){
requestPIN();
}
console.log('---> request body: ' + _body);
request.pipe(_request
.post(opt, function(error, response, body) {
if (!error)
{
console.log('All OK');
console.log('---> response statusCode:' + response.statusCode);
console.log('---> response header:\n' + JSON.stringify(response.headers, true, 2));
console.log('---> response body:\n' + body);
} else {
console.log('Error: ' + error.message + ' (code: ' + error.code + ')');
response.writeHead(error.code, {'content-type': 'text/xml'});
response.write('Error: ' + error.message);
response.end();
}
})
.on('error', function(error, response) {
console.log('Error: ' + error.message + ' (code: ' + error.code + ')');
response.writeHead(error.code, {'content-type': 'text/xml'});
response.write('Error: ' + error.message);
response.end();
})
).pipe(response);
});
}).listen(8000, function() {
console.log("server ready on port 8000");
});