Node Server With NGINX - javascript

I have node js server that has a server which listens 8000 port and a socket.io connection working on that server. This socket connection creates a communication with a ReactJS app which is not a point of this question. So I have 2 project folders
1. project-server
2. project-web-react
Project server only answers socketio request and does not render a HTML or something else. It only works on terminal. I want to ask whether is it useful to encapsulate my project-server with Nginx? So the requests are handled by Nginx ? Or is it out of the Nginx's purpose?

I would never have an application server run directly connected through internet since there are always a bunch of unknowns with them (scaling, standard compliance etc), so I would recommend you to run a proxy like nginx in front of your app. This also makes it easy to add certificates and do load balancing / caching. It just adds flexibility and some security.

Related

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

How to give the server port to client

I have a node server with several node projects. I use nginx to get them all responding on port 80. Now, this works for the initial http request. For the websockets, I need to use the direct server port. To keep everything alive while developing I would like to try this, projects will have a dev and live version. Once de dev is stable, I will copy it to the live folder. The live folder is runned by a systemctl script where I define a difrent port to the live version so I can dev without taking the live down. The problem I encounter now is, how can I get the running server port in my client side Javascript so that the dev page connect to the dev port and visa versa?
currently I'm only using express, socket.io and mysql. I have no further npm packages installed. I searched allot but there is not to mush I can find. I found how to connect the socket to the page url but I cannot use that because that URL will always be on port 80. Further I found allot of huge packages that has no use for me since the original page is just static, the dynamics all run over websockets.
Is there any way to parse the port number in the clients .js file like I could do fairly easy in php? And if so, what would be the most efficient way. I could let javascript check if the page uses the live or dev URL but I would prefer not to hardcode my dev URL into JS where it is for everyone to see.
run a third node.js socket.io server program, all your clients connect to this server first.
In this simple node.js program, determine the type of clients by any means. e.g. different user id for dev/production users
send the server url and port to your client according to its type (dev or production)
you may also use this technique to separate your users to different production servers.

SSL With node / IIS

My current setup with the company i've joined is IIS is being used as our main web server, using a CA signed certificate. I've got access to the certificate.pdx aswell as the private key.
I've setup a https node API server to handle some real time stuff / act as a simple additional middle layer, but my website is being served through IIS. The flow of the application is something like this.
1) Visit web page where IIS will then serve it
2) Click on a button, which then makes a GET request to my node server
IIS NODE
E.g 100.10.10.10:3000/mypage > Click Button > 100.10.10.10:4000/myGetRequest
At the minute i'm just using self signed in my development environment, and manually accepting the certificates.
My question is can I just use the same certificate that IIS is using on my node HTTPS server, or do I need a different one?
It would be best if the IIS could proxy the requests to your Node app. That way your Node app wouldn't even need a certificate (if it's on localhost). See this answer for more details:
Configuring HTTPS for Express and Nginx
It uses the example of nginx but you should be able to proxy the requests with IIS as well. See this tutorial:
Setup IIS with URL Rewrite as a reverse proxy for real world apps

How do you build node.js dynamic web server? (upgrade server without notice)

I know that node.js server caches modules. So, after starting the server all files get "compiled" and all your changes to code can take effect after restart the server.
But if there are always hundreds of users online on website - how do you make those changes (restart server) in a way your hundreds of client won't notice any trouble, downtime?
Please, give me some guide and (your own) examples about (I guess) scalability, balancing load on servers etc, so I can make awesome large and dynamic website with node.js too.
The best way to accomplish continuous uptime with Node.JS is to have 2 Node servers running and proxy to them using nginx and upstream providers. That way you can restart one whilst the other takes the traffic load then do the same to the other node server.
Your nginx configuration would use something similar to the below:
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
For more information about nginx proxying and upstream providers see the nginx documentation: http://nginx.org/en/docs/http/ngx_http_upstream_module.html

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.

Categories