rest call from node fails - node.js

I am building a node.js/express app which makes remote calls to other internal web application (ASP.NET WEB API) to consume json from it. We are in a corporate network. Here is the strange issue.
On my Mac OSX (Mavericks), I can curl from shell and get json from http://our_internal_host:9991/connections. I can also type this URL and see the JSON response from the browser.
When I run this express app locally and request the route which makes the remote call, I see this error on the console. The route handler logs this message below and browser hangs.
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
Node process cannot make a connection to that address. I also have a Windows 7 machine at work and I do not encounter this issue on my Windows 7 machine. When I run the same node app on Windows 7, I don't have any issues.
I am not sure how to troubleshoot the issue...
PS: A colleague of mine who has the same setup doesn't have this issue. We compared DNS configs but our setup looks to be same.
Any pointers to troubleshoot this issue is much appreciated. I know this is environment specific issue but not sure where to start.
Thanks
EDIT #1
Route handler making the remote call which logs the error above...
var http = require('http');
var options = {
host: 'our_internal_host',
port: 9991,
path: '/analytics'
};
http.get(options, function(res) {
// removed response code
console.log(res);
}).on('error', function(e) {
console.log(e);
});

It turns out my Mac's IP was on a different subnet than other machines.

Related

How to write and test dynamic server for Office.js add-in?

I'm developing an Office.js add-in for Excel, and I'm kind of lost on how to create the server side of the application and test it on localhost.
I've created the add-in project/structure using Yo Generator, and I'm using gulp to test it on localhost (port:8443). Using this approach, I was able to successfully load my add-in and test the client-side of the same. Also, I've tested a http request to a static json file and it worked fine.
The issue is that I need to run code on server side for dealing with files and doing some processing, and I simply can't find a way to do that.
I've already tried to start a localhost server on a different port (port:8000) using the code bellow and node command:
var https = require('https');
var fs = require('fs');
var httpsOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}
https.createServer(httpsOptions, app).listen(8000);
The server started ok, but as my application is running on port:8443, I'm unable to do cross origin requests (which I understand would also not work on a production environment).
I also tried to start add-in server on port:8443 using gulp serve-static command, and then start a server listening on the same port:8443 using node command, but this results on the error bellow:
Error: listen EADDRINUSE 127.0.0.1:8443
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1234:14)
at listen (net.js:1270:10)
at net.js:1379:9
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:64:16)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:83:10)
May someone please help on how to get this working?
Please let me know if any further information is required.
Thanks in advance.
First, you can't have two servers listening on the same port simulateneously. That's why you're getting the Address in Use error.
Second, I am in a similar situation like you and my thinking here was, that the way to go would be writing a server that provides an API (e.g. REST). Then, the javascript code that get's loaded into office (for starters the App.js in your yo office generated project) makes requests to this API.

Bluemix NodeJS ENOTFOUND

I have a bluemix node js application which communicates with a server. I have test and production environment. On the development environment we communicate to the test server, and I get a node js error.
When I change the server URL to the production server everything is ok.
When I run the app on localhost and connect to the test server everything is ok too.
So my problem is only on bluemix environment with communication to my company test server. Error is:
{
"code": "ENOTFOUND",
"errno": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "www.xxxxxxxxx.cz"
}
Hostname in error is masked.
From the exception, I think the failing code is doing a dns lookup. I wrote this sample code and found that the error is similar or same.
var dns = require('dns');
dns.lookup('non-existent server', function(e, a) {
console.log(e);
});
And the output is:
bash-4.1$ node h.js
{ [Error: getaddrinfo ENOTFOUND non-existent server]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'non-existent server' }
bash-4.1$
Problem determination steps would be:
ping your target server from a machine which has outbound access - to make sure the server is present. If not, resolve that problem.
Logon to bluemix debug console
ping your target server. If it does not respond, there is a wall between bluemix and the target. If it responds, try this test case. If that too works, we will have to debug further, I can be of further help.
Bluemix debug console is obtained through:
Export an environment variable "BLUEMIX_APP_MGMT_ENABLE" with value "shell"
Restage the app.
Login into the web shell in browser at https://your-app-url/bluemix-debug/shell/ using your Bluemix user credentials
Hope this helps.

Issue with HTTP request using node through TOR

I am relatively new to both node and TOR. I'm trying to send a request using a node application through TOR. Ive set up a basic app using socks5-HTTP-client. Also, TOR is installed (sudo port install tor) and I ran the server using tor..
Executing the app, node gives me the following error
Error: SOCKS connection failed. General SOCKS server failure.
at Socket.<anonymous> (/Users/MyName/node_modules/socks5-client/lib/Socket.js:199:12)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
The TOR server reports the following to me:
[warn] Rejecting SOCKS request for anonymous connection to private address [scrubbed].
The topic I found here does not seem to apply to this case. Also, this earlier article does not answer my question. Neither can I find any difference between the example given by the developer of the socks5 module and my case.
I am unsure whether this is an issue with my configuration in node or my TOR configuration.
Below is the JS code
var shttp = require('socks5-http-client');
var config =
{ url: 'http://whatismyipaddress.com/', // I'd like to see a different IP on every request, that should be the end result, correct?
socksHost: '127.0.0.1',
socksPort: 9050
}
shttp.get(config, function(res) {
res.setEncoding('utf8');
res.on('readable', function() {
console.log(res.read());
});
});
Also, in my torrc.sample configuration file, I made sure the following is set up:
SOCKSPort 127.0.0.1:9050
Also, my SOCKSPolicy is set up as follows:
SOCKSPolicy accept 192.168.0.0/16
SOCKSPolicy accept6 FC00::/7
SOCKSPolicy reject *
At the time of trying, my internal IP address was 192.168.0.11, seems fine to me.
Any help would be appreciated!
You are getting that error because the URL isn't getting passed to the request properly so it is trying to issue a SOCKS request to localhost instead of the site you want to visit.
URLs are parsed using url.parse.
Try changing your code to:
var shttp = require('socks5-http-client');
var url = require('url');
var config = url.parse('http://whatismyipaddress.com/');
config.socksHost = '127.0.0.1';
config.socksPort = 9050;
shttp.get(config, function(res) {
res.setEncoding('utf8');
res.on('readable', function() {
console.log(res.read());
});
});
Also, it appears whatismyipaddress.com might be blocking Tor as I don't get a response back from them. Try ipchicken.com or some other site. You may need to set a User-Agent header to get the former site to reply, but this code change does work.

Why would I be getting `Error: write EPROTO` when making an HTTPS request in an Electron app?

I've been pounding my head against the wall for days on this so I am turning to the smart folks at Stackoverflow to help. Here's the deal:
System Details
Node Version (can't be changed due to Electron dependencies): v4.1.1
Electron Version: v0.34.3
OS Version: Mac OSX Yosemite 10.10.5 (14F1021)
Issue Description
I'm building an Electron app that has to communicate with my company's application server. The server connection has to go over HTTPS. I'm using Node's built-in https module. When making a request to the server I'm getting the following error:
{ [Error: write EPROTO]
code: 'EPROTO',
errno: 'EPROTO',
syscall: 'write',
address: undefined }
I've done a ton of Googling on this and most everything I've found points to proxies but I'm not using a proxy. I've tried the following:
Setting rejectUnauthorized: false in the options hash
Modifying the secureProtocol option (no results)
Attempting to set the --tls-cipher-list (no idea what I'm doing there)
I can make the request over curl without issue. Unfortunately, I can't post the actual URL I'm making requests to.
Sample Code
Here's some sample code (Coffeescript) that illustrates the issue:
https = require 'https'
options = {
host: '[Application URL]'
path: '/'
method: 'GET'
port: 443
}
options.agent = new https.Agent(options)
callback = (response) ->
str = ''
console.log response
console.log "STATUS: #{response.statusCode}"
console.log "HEADERS: #{JSON.stringify(response.headers)}"
response.setEncoding 'utf-8'
response.on 'data', (chunk) -> str += chunk
response.on 'end', -> console.log str
makeRequest = ->
req = https.request options, callback
req.on 'error', (err) ->
console.log err
req.end()
makeRequest()
Does anyone have any idea what could be causing this issue? Is it a Node issue or something with the configuration of the application server? This bug is killing me and preventing me from hitting a milestone at work so any help would be greatly appreciated. Thanks!
To resolve this issue you are required to set https-proxy, which can be done with the following command:
npm config set https-proxy http://proxy.example.com:8080

socket.io on openshift with angular-fullstack

I generated a node.js app using Yeomans angular-fullstack generator.
Everything worked fine except the use of socket.io. After uploading my app to openshift using angular-fullstack:openshift, I got informed that I have to:
Openshift websockets use port 8000, you will need to update the client
to connect to the correct port for sockets to work.
in /client/app/components/socket/socket.service: var ioSocket = io.connect('http://my-domain.rhcloud.com/:8000')"
I dont know where to do this. I am using socket.io version 1.0.6 which is shown inside package.json file.
==> app-root/logs/nodejs.log <==
/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41
socket.address = socket.handshake.address.address + ':' +
^
TypeError: Cannot read property 'address' of null
at Namespace. (/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41:46)
at Namespace.EventEmitter.emit (events.js:95:17)
at Namespace.emit (/var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:205:10)
at /var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:172:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server/app.js exited with code 8
DEBUG: Starting child process with 'node server/app.js'
By the way, the app, including socket.io, works fine on my local development machine!
Thanks for any help!
Fall.Guy
My working solution is now:
I changed the creation of the openshift app to "not create a scalable app" (I disabled the -s switch in the yo angular-fullstack:openshift command)
After that, I changed the connection of socket.io to:
var ioSocket = io('http://my-domain.rhcloud.com:8000', {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
});
in the "/client/components/socket/socket.service.js" file.
Socket.io works now fine!
I'm not sure why openshift requires this, but you just need to modify your client code.
In your client side code, you should have something like this:
var socket = io();
You'll apparently need to update it to:
var socket = io('http://mywebsite.com:8000');
It's worth noting that there are plenty of other hosting providers (even free) which don't require you to jump through these hoops.

Resources