Node.js Net.Socket() to connect to net.tcp web service - node.js

So I have a web service running on port 7001 over TCP on:
net.tcp://www.myurl.com:7001/my/webservice
and I want to connect to it using net.Socket:
client.connect(7001, 'myurl.mine.com/my/webservice', function () {
console.log('Connected');
client.write(msg);
});
When i do the above, it gives an exception:
Error: getaddrinfo ENOTFOUND <<my url>>
at GetAddrInfoReqWrap.onLookup [as oncomplete]
when i try connect to it without the /my/webservice, it connects fine and doesnt give an error at the client.connect() stage, but obviously cant find that endpoint so it gives another error when i try to do the client.write()
Any one any idea how to use net.Socket against a web service with a url that isnt just myurl.com:7001 but actually contains a route like myurl.com:7001/my/webservice?

According to node.js docs the version of the method you are using expects only a hostname for a second argument. You are providing hostname + path
In your case I would recommend using a more generalized version of net.connect which accepts an object with parameters. So your code will look something like this:
client.connect({
host: 'myurl.mine.com',
port: 7001,
path: '/my/webservice'
},
function () {
console.log('Connected');
client.write(msg);
}
);

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.

nodejs and mongodb remote access through application

I have a web app setup using nodejs and mongodb and backbonejs.
When I run my app remotely and try to redirect to a different route through the uri, for example: http://www.myapp.com/route I get a net error.
In my route.js file I have the following:
var mongodb = require('mongodb');
var db = new mongodb.Db('dbname', new mongodb.Server('xx.xx.xxx.xxx', 27017, {}));
where xx.xx.xxx.xxx is my public ip. However, when I change this to anything other than "localhost" and try to run node server.js in the root of my app I get the following error:
Listening on port 3000...
/var/www/html/pages/node_modules/mongodb/lib/server.js:236
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1012:19)
I have already uncommented bindIp in my /etc/mongod.conf file as suggested in other posts. I did this only to test and just to get it to work, but I plan to go back and play with the ip tables if I get this to work.
How can I access my app remotely?

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.

Openshift Zombiejs

I have a Node.js/Express application which I'm trying to deploy to Openshift.
The server itself starts up properly and serves almost everything.
One request to the server opens up a Zombiejs browser, which then crawls some webpages.
When the browser tries to visit a webpage however, I get this error:
{ [Error: bind EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'bind' }
Possibly unhandled Error: bind EACCES
at errnoException (net.js:901:11)
at connect (net.js:747:21)
at net.js:842:9
at asyncCallback (dns.js:68:16)
at Object.onanswer [as oncomplete] (dns.js:121:9)
The browser fails as soon as I call visit:
browser.visit(menus_url).then(function () {
// do things
});
Before doing the above, I start an Express-based server as follows:
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP;
app.listen(server_port, server_ip_address, function() {
console.log('Server running on port ' + server_port);
});
The server starts up fine, however as soon as I make a request that calls browser.visit() the error is raised.
Update 11/15
I still haven't found a solution to this issue.
When the browser is created, it tries to use the ip address of 0.0.0.0 to bind to (https://github.com/assaf/zombie/blob/master/src/zombie/browser.coffee#L1224) by default, which is not available on OpenShift for your use, you need it to bind to your OPENSHIFT_NODEJS_IP ip address, just like you use for your express server. It looks like, at the top of the file that I linked to (https://github.com/assaf/zombie/blob/master/src/zombie/browser.coffee#L42), that one of the Browser options you can pass in is 'localAddress' when creating the Browser object, which should be the OPENSHIFT_NODEJS_IP. I think that will get it fixed up for you.

Can't Connect To A Telnet Server From Node.js

I have a telnet server embedded in a C++ app that I can connect to with no problem
using telnet.
I want to write a node application that will connect to my server and I have tried
this
var net = require('net');
var port = 6502
var host = '127.0.0.1'
var socket = net.connect(port,host, function() {
console.log("Sending data");
socket.write("hello\r\n")
socket.on("data", function (data) {
console.log("received data");
console.log( data.toString() );
socket.end();
})
})
socket.on("error", function(err) {
console.log("Error");
console.log(err);
})
Unfortunately what I get back is this
> node test.js
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
What's really odd is if I set up a simple echo server with node everything works
fine. Here's the working echo server code:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(6502, '127.0.0.1');
and from that I get
Sending data
received data
Echo server
hello
Is there any reason why:
I can telnet into my app fine
I can connect from node to my node echo server on the same port
I get a connection refused if I connect from my node app to my app
Extra Info
On OSX (mavericks)
Node version 0.10.28
Telnet server in C++ is provided via embedded lua and luasocket (lua 5.1)
Solved!
The issue is the code in my app server was binding to localhost and by default
that binds to the IPV6 address of ::1
Passing a host of localhost to net.connect assumes IPV4 and doesn't work.
The mac command line telnet and nc both work fine with this and connect correctly.
Two solutions:
App binds to 127.0.0.1 and localhost in node works fine
Set host address to ::1 in test.js and it connects via ipv6
All fixed now though :)
gaz
Looks like you forgot to tell the server to listen in your code. It throws a connection refused error because there is nothing to connect to...
Add this at the end: server.listen(port);
The prototype for net.connect is (options, callback)
See http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener
I would then suggest to test your code against a standard telnet server to see how it behaves, and finally I would strongly recommend the use of jshint or jslint.

Resources