node.js - communicate with TCP server (data == JSON) - json

I'm new to node.js, dived into it during the last weekend and had fun with various examples and small tutorials.
Now I'd like to start a little project for my local area network and have a few questions to get myself into the right direction.
Setup:
I've got a server service running on my lan. It's possible to communicate with that service via TCP and/or HTTP (it's possible to enable or disable TCP or HTTP or both of them) on a specific port and sends and receives data via JSON on a request.
What I basically want to do is to create a webinterface based on node.js for that service, to receive and send JSON data with a webbrowser from and to that service.
Problem:
I already do know how to setup an http server based on node.js. But right now I'm stuck in finding an idea how to create a client based on node.js which stands between the service and the webbrowser client to pass through data from client to server and vise versa. Something like a router or proxy.
Here is a basic scheme based on the client's (webbrowser) point of view:
Send: Webbrowser requests -> node.js routes -> service receives
Receive: Webbrowser receives <- node.js routes <- service responds
Questions:
- Go for TCP or HTTP? (maybe disabling the HTTP Server would spare some ressources) - maybe already answered by this post
- Are there any node.js packages that would fit my needs?
- Go for a framework (expressions?) or would plain node.js be just enough?
- Any hints appreciated :)
edit:
- Is it possible to bind a network device like eth0 inside node.js instead of defining the ip address?
Thanks for your help && best regards
cefra

There's no reason why you can't have a REST HTTP service.
Use something like express to handle routing.
If I understand your problem correctly then you have a webservice written in "foobar" on a TCP port somewhere you can connect to with node.
So if your using express you would write something like
app.get("/resources/", function(req, res) {
var socket = new net.Socket();
socket.connect(port, host, function() {
socket.on("data", function(json) {
res.contentType("json");
res.send(json);
socket.end();
});
socket.write(...);
});
});
So basically you've written a http middleman that contacts your service over TCP then writes the data down the response of your HTTP request.

Related

Can I make an reactjs axios request internally by https://localhost:1337?

I have two servers, which are both stayed at the same host but different port.
One is reactjs server hosted at port 3000.
Another one is database server with API interface hosted at port 1337.
I use Axios to make api request to the database, but it only works when I make call with Public IP address (for example: axios.get("http://215.128.23.16:1337/collection/"). When I change to localhost, it shows error. (shown in attached pictures)
What I want is to make API request internally, in order to restrict the IP address of accessing the database to only localhost connection.
Here are the steps I want to achieve: Client Side make a API call from browser -> Reactjs server receive the call -> Reactjs server send API request to database server internally (then I can restrict the IP access of accessing database to localhost) -> return result
Is this possible???

webmethods IS pub.client http not following http client standards?

The issue: Webmethods HTTP client is calling the wrong endpoint on my Apache server configured with multiple virtual hosts, based on DNS.
What I think is happening: I think Webmethods HTTP client may be looking up the IP address and using that to perform HTTP operations instead of using the DNS name, which is causing the Apache server to identify it as a request to the main virtual server, not the desired one.
Question: So, how can I make webmethods use the DNS name instead of the IP? Is my theory about the Webmethods HTTP client correct? As far as I can tell this is a very non-standard approach to HTTP Client design.
Here is how it is configured to help you better understand:
Apache ->
host.example.com => /var/www/host/html
host2.example.com => /var/www/host2/html
curl -v http://host.example.com and curl -v http://host2.example.com appropriately return documents from their respective directories.
Configuring pub.client:http with http://host2.example.com causes the webmethods IS server to request http://host.example.com documents (obviously leading to a 404: Not Found).
Note that obviously the system is not returning documents like HTML but rather serving dynamic content.
The comment from Progman is the clue here - basically in order to direct Apache to call your virtual server, the Host header must be given with the expected value. In my example, that would be Host: host2.example.com. I was having webmethods IS copy over the headers exactly like I was posting them from curl, and it was sending Host: localhost:5555 over to my proxied server. I simply created a pipeline Map operation and hardcoded it and it is working fine now.
The oddity to me seems to be that pub.client:http didn't auto-set the Host header for me based on the 'url' value, which is what I would have expected.

How to send json data over http

I am new to http protocol. When we are sending json message over http to server, How we need to send ?
we need to send the data from different port each time
OR
we can send data form a single port in each time.
If I want to use existing connection to send data in future then whether it is possible or not ?
There is no reason why you would create a TCP socket for each piece of data you want to send — and this has nothing to do with HTTP — and particularly not through a different port each time. In fact, once you hace the socket created and you have connected to the server you should in principle always talk to the server through that socket.
Also, the HTTP protocol uses the port 80, and HTTPS uses 443. That number does not change on demand. Of course you can send HTTP requests through any available port you want and some services even run on special ports using HTTP as the communication protocol but normaly HTTP is 80. See the /etc/services file on linux and read about getaddrinfo().

Connecting to HTML5 Websocket

I'm a bit confused about HTML5 Websockets. I've looked at numerous tutorials out there and a lot of them have different variations of connecting using different ports. What do these ports mean?
Adobe for instance, uses this:
new WebSocket('ws://localhost:1740');
Then another tutorial has this where no ports are required:
new WebSocket("ws://www.websockets.org");
And finally a third tutorial has a port, but it's completely different:
new WebSocket("ws://localhost:8080/echo");
My question would be, why do these vary? How do I know which ports to connect to? Also, I've attempted to do my own connection:
var ws = new WebSocket("ws://test.ontarget-network.com/");
But I get the following error: Unexpected response code: 200
I've tested around and tried connecting to various other "ports" (not knowing what I'm doing obviously, typing in random numbers) and this error would disappear, however, my code
ws.onopen = function(){
alert("Connection Established");
};
would not execute.
I'm trying to fully understand HTML5's Websockets API so I can experiment and create more dynamic applications. Thanks for the help.
The server should have an endpoint that accepts WebSocket connections. So, if that endpoint is /echo you would want to connect to:
ws://localhost:8080/echo/websocket
You will get the Unexpected response code: 200 error if you exclude the /websocket suffix after the endpoint. I was having the same confusion and this link cleared things up a bit for me.
The following comes from the latest WebSocket draft:
By default the WebSocket protocol uses port 80 for regular WebSocket
connections and port 443 for WebSocket connections tunneled over TLS
[RFC2818].
Really though, you should be able to use any valid port not in use. As long as clients are trying to connect to the same port that the server-side script opens for the socket connection, you should be fine.
A quick note on ports:
Port 80 is the HTTP port.
Port 8080 is the alternate HTTP port.
Port 443 is the HTTPS (i.e., HTTP with TLS) port.
Port 1740 in the Adobe code seems like some random port not already in use by other services.
For a full list of preset ports, please see the following:
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
As for your "Unexpected response code: 200" error, I'm guessing that the WebSocket URL you're using on the client side is not pointing to a valid server-side script, but that's hard to comment on without more info.
I had the same issue, But to survive with
Unexpected response code: 200
You need to have either server-side script to handle the web socket, or you can use Node.js to build a you server script.
for the sake of education you can try to biuld your own websocket sever script.
Actually there is something else... You can not open a connection to every port since there is a list of blocked ports in every browser. I remember seeing the full list of ports in 'The tangled Web' from Michal Zalewski; however, I think a quick google will show this also.

How to retrieve the http response headers in a non-AIR application

Access HTTP response headers in for flash.net.URLLoader object?
I found the above question that seemed to have a solution for an AIR application but not for my non-AIR flex application? Is it even possible?
If it is not possible in native as3 you could parse it yourself. Open a socket connection using the Socket class and connect to the url/domain using port 80.
After you connected you can read out the socket buffer and parse the string to get the response headers.
You can follow the Telnet example on http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b0-181c51321220efd9d1c-8000.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7cf7 and change the port numbers to 80.
I believe I was reaching a little beyond the abilities of a non-air flash application. I wound up retrieving the headers server side and then using the external interface callback functionality to use what I knew about the headers flex-side.