nodejs/curl http request is slow compared to postman rest client - javascript

I call a Rest API from node.js and Postman-Rest Client (chrome app).
Always using the same computer, same endpoint and same parameters.
In node.js I get ~400ms for http and ~700ms for https.
In Postman I get ~250ms for http and https.
Libraries I used in node.js: http, https, request, kinvey (node client of the api). All of them got the the same result more or less.
Why my node is slower?
node.js 0.10.36
Edit:
it's not about node, i get the same result with curl.
when running in browser using XMLHttpRequest i get better times.
from analysing the logs I found that TCP connect and SSL handshake takes about 500ms. It may be that TCP connect and SSL handshake works differently in browser.

Here you can find some help.
I am also facing issues with nodejs application server on linux instance. But dont blame nodejs as real issue is in linux systems.
Issue : any outbound request from machine need to find the domain , hence it always lookup the DNS entries & always handshakes with secure/unsecure protocols. This is actually the time taking scenario.
You can verify by doing CURL request.
curl --trace-time -v https://outboundserver.com
Solution : We have to White List IP of outbound server in our own DNS entries. take a look below ...
IN Windows : automatically does the mapping of servers in own dns entries.
IN Linux : We have to manually add the hosts corresponding to IP Address.
Manually add an entry in /etc/hosts file below any configuration like localhost.
127.0.0.1 localhost
outbound.servers.ip.address www.outboundserver.com
Finally check with the curl request again, it should give faster response.
curl --trace-time -v https://outboundserver.com

Related

Graphql server - Can't access via IP address url (localhost works fine)

I would like to link my Expo(React Native) app to local GraphQL backend,
http://localhost:4000/ <- Works fine
http://192.168.X.X:4000/ <- This doesn't work
I do see the Apollo Studio page, but it comes up with the below message:
Unable to reach server
Network requests from Studio on HTTPS to your HTTP endpoint are not secure,
so we cannot introspect your endpoint.
https://studio.apollographql.com/sandbox/explorer
Apollo Studio has a special exception that allows it to communicate with "localhost" over http, but all other endpoints must be over https.
If you (like me) are running virtual machines on your local machine and need to use Apollo Studio, the only solutions are to make the connection https somehow, or forward a port in your host OS so that you can access it via localhost. How to do THAT depends on your OS.

Calling HTTP Node server inside HTTPS instance, SSL error

I'm trying to connect to a socket.io inside a Node server from a React client.
Both, the React client and the instance that the Node server is located (it's a microservice, there's also a Java container running there, both within a separated docker container) have https protocols. React client is inside a s3 bucket.
The thing is, Node server is HTTP only, has no certificate, and it's causing the request to the socket.io to fail.
The connection happens without any trouble inside de develop EC2, which is not HTTPS, also running normally with localhost.
Cors are enabled.
Is there a way, without having to turn Node serve into HTTPS, to make this requests not return SSL erros?
If not, what is the easiest way to turn it into a HTTPS server to run along with the Java server?
Thanks a lot!
A very short answer. Try using nginx as WebSocket proxy for you socket.io server.
Here are couple links:
nginx as WebSocket proxy
SSL configuration to make it an https
Socket.io official configuration

Websocket returns status 200 instead of 101 (flask socket.io)

I've done quite a bit of reading around to try and solve this issue but I'm still stuck. My problem is with trying to get the websocket handshake to complete using socket.io client side and flask_socket.io server side.
I can run the flask development server on my local machine using:
app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
socketio = SocketIO(app)
socketio.run(app)
...
and if I point chrome to localhost:5000 and press the button which I have linked to opening a websocket it works fine and chrome network tab shows status 101.
However on uploading the code to a remote machine and again using flask's development server but changing the port to 80,
socketio.run(app,host='0.0.0.0',port=80)
the websocket handshake stops working and gives status 200 instead.
WebSocket connection to 'ws://example.com/socket.io/?EIO=3&transport=websocket&sid=cfb1949b243b42578fe422782a0db359' failed: Error during WebSocket handshake: Unexpected response code: 200
All websocket messages are now sent over xhr polling instead of inside the websocket frame.
I've followed all of the guides I can find on google relating to this but with no success. I was previously using nginx and gunicorn and followed the advice to change the nginx conf to allow upgrade to websockets but that didn't solve the problem. So I simplified to using the flask development server but I still haven't been able to get a successful handshake.
I found a solution after finding a similar problem posted here https://nolanlawson.com/2013/05/31/web-sockets-with-socket-io-node-js-and-nginx-port-80-considered-harmful/#comment-85425.
It turns out many public wifi networks block websockets when they are running on port 80. However often ports 443 and 8080 are not blocked. You can find out which ports are blocked from this website: http://websocketstest.com/. I just moved the address of my websocket from example.com to example.com:8080.
I ran into a similar problem but due to a different reason. When using flask-socketio you have to install gevent-websocket or else it's going to use long-polling and you'll see a lot of 200 responses. Thus, my solution to that problem was just.
pip install gevent-websocket

How to run node.js in my web site server not my pc local server

Last 2 days I spent more time and read 50+ articles and video to understand node.js and after installation now I can see the result in browser by http//:localhost:3000/ But I have confused in many case that I describe below.
I do all of my work in my share hosting server where I my keep my web site: www.myweb.com
In every article about node.js, they are teaching how to get a result by below code in a browser by http//:localhost:3000/ in local pc server.
test.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
But My Question:
If I use http//:www.myweb.com/test.js` in my browser, What will be the above code?
In case of local pc we write on npm node test.js, But In case of hosting server when any clint open the page like http//:www.myweb.com/test.js How to work it?
In case of php we used include ("head.php") to got something from that page But In this case How to make a call on node.js.
Well, what you need to do is understand how http web servers works.
Usually, on your remote machine (your server), you have an instance of a web server (ex : apache) running, which is listening to port 80 (standard port for http requests). It will handle every request made on that port, and manage routing to use the correct php/html file.
Then, it will run the php code server-side, to render an html file and serve it to the server. So the client will not see the php code at all.
Let's talk about Node.js. Node is an application that runs javascript code server-side, and can run an http server with using some modules. But the javascript code will never be shown to your client, he will only get the http response you send him (typically, the html page).
So now, with node.js, you need to do the same as the apache server did, by creating the http server. First, what you have to know is that not that many website host are offering node.js, or even console access. They usually serve the php/html files you put in the configured folder, and that's basically it. What you need is either a virtual machine, or a server on which you can install node.js and run it, or use a node.js hosting service, like heroku or nodejitsu to host your node.js http server.
So, to create the node.js http server, you need to create an http server (as you did in your code), and make it listen to port 80. Now, every http request send to your server will be handled by your node.js instance. Then, you can do anything you want with that request.
I hope I haven't been to messy.
You need to install NodeJS on the server. If this is shared hosting where you cannot install additional software then you will be unable to use NodeJS. In that case contact support of your web hosting company and inquire about NodeJS support.
On the other hand, if you do have root user or super user rights on a system, you can install NodeJS. For example for on CentOS/RHEL systems you can install using yum with the following commands.
sudo yum install epel-release
sudo yum install npm
For some of the other distributions of Linux: http://ask.xmodulo.com/install-node-js-linux.html
To access Node applications from your PC to the server, you also need to open a port in the server firewall that your Node aplication uses.

Node.js on Azure Worker Role w/ SSL results in ERR_SSL_PROTOCOL_ERROR

I have a WorkerRole configured to start node.exe via the Runtime/EntryPoint/ProgramEntryPoint element in the csdef and have a HttpsIn EndPoint configured for https on port 443 w/ a valid certificate. I'm also setting the PORT environment variable in Runtime/Environment which is used by node to listen on for incoming requests.
When I start the service (either in local dev fabric or in Azure) and try to hit the service I get the following error:
SSL connection error
Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.
Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.
I have verified that node.exe is indeed started when the service starts, and if I look up the local port in the Compute Emulator, usually something like:
http://localhost:444
I am able to successfully hit node directly with that using my browser. I am also able to hit node through Azure when SSL is not configured.
What am I missing? Thanks!
The issue was that I was using the http module instead of the https module when starting the web server in Node. Works once I started the https server using the ssl certificate.
I was following a guide for SSL w/ Node in a WebRole, which requires a different set up than SSL w/ Node in WorkerRole.

Categories