How build to deploy node app to http server - node.js

Is there any way to use express or other nodejs librarys to to design an api and deploy it pasting files as a resource path in a web server just like react.
My web server just listen on ports 80 and 443.
I have pages on domain.com/page1 domain.com/page2.
and i want to deploy an api listening on domain.com/page3 but i cant install a nodejs server and proxy requests to page3 to it.
Thanks a lot

There can be only one web server listening on a given port. You can directly "listen to a path", only a port.
So, if you already have an existing web server running on ports 80 and 443, then these are your options.
Modify your existing web server to add your API server code to it so it can handle requests for http://yourdomain.com/page3 and https://yourdomain.com/page3 directly with your API.
You can add some middleware to your existing web server to make it a proxy for http://yourdomain.com/page3 that will redirect requests to your http://yourdomain.com:3000/page3 Express-based API server.
Run your own Express-based API server on your own separate port and access the API server directly via the separate port such as http://yourdomain.com:3000/page3.
You can install a proxy such as nginx in front of your existing web server and have it redirect incoming requests to http://yourdomain.com/page3 and https://yourdomain.com/page3 to the separate port that your Express-based API server is running on.

Related

Connect domain to nodejs server hosted on vps instead on ip + port

i have 2 react apps and a nodejs server (express and websocket) running on port 8000.
the express server is for generating random links and the websocket for sending real-time messages between the apps.
my apps worked perfectly after hosting on my vps server but after connecting letsencrypt ssl to both react apps, they don't work anymore because i have to connect to a websocket with secure connection.
so i want to connect the nodejs server with a subdomain i have from namecheap.
i have tried pointing the subdomain i got for the server to the ip of the vps but i don't know what to do after that.
should i create a new file just like i did for the previous apps in the sites-available directory, listen to port 8000, set server name to the intended subdomain and run a proxypass to the same subdomain?
This is my first time hosting on a vps server, i'm very confused at the point but my server works well when i just type in ip:8000 on my brower

How to make an HTTP request from a remote web client hosted by an npm server to a private port of a backend server running behind the Linux Firewall

There is a front-end npm server hosted on a public port 80 in my production environment. I can launch a remote web browser client of this front-end server using its public hostname, e.g., http://hostname:80, and successfully load the webpage.
The Javascript in this app makes HTTP GET/POST requests to a back-end server to fetch some data on the URL: http://hostname:5000. This back-end server is running on the same production environment but on a private port, e.g., 5000, i.e., this port will not be visible outside the firewall.
As I understand it, this HTTP request is essentially made from the remote web browser client sitting outside the firewall. Due to the Firewall (UFW) policy, any request made from this client on private port 5000 gets blocked.
I do not want to allow the private port 5000 in the UFW, and I do not want to run the back-end server on a public port of the production server.
What are the solutions for this?
I have heard about the Nginx proxy server which redirects client connections on a public port (80) to a Node application running on a different port (3000).
Reference: https://blog.logrocket.com/how-to-run-a-node-js-server-with-nginx/
However, I am not certain if the Nginx server would be able to handle the client requests beyond the UFW rules.
The majority of request information is sent to the backend from the proxy.
A request sent through the nginx proxy will act like a request directly to the backend.
Some fields may not be passed, for example:
From nginx.org:
By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed

does socket.io server need to be seperate from backend when deploying?

I am building react app. I have my client folder, and my backend folder that contains all my mongo db models, routes, functions etc...
I know realize that my app needs to use socket.io
My frontend is on localhost:3000 and my backend is on localhost:5000
My understanding is that socket.io needs its own port.
Does this mean when I deploy to heroku I need to deploy a backend server, frontend server, and a socket.io server?
My understanding is that socket.io needs its own port.
This is incorrect. socket.io can use the same port as your backend just fine. Incoming requests to create a socket.io connection can be distinguished from other web requests via a custom header that the underlying webSocket connection protocol uses. This allows socket.io/webSocket and your http server to use the exact same port.
Does this mean when I deploy to heroku I need to deploy a backend server, frontend server, and a socket.io server?
No. You can still just have frontend server and backend server and the backend server can handle both your backend requests and the socket.io connections.

What is recommended way to set up a web server?

I have a server and I am designing some web applications. I use React as frontend framework and Rust Actix as backend framework. The backend program listens at 8000 port, and can be reached by my_domain:8000/api/xxxx.
I think I can use a web server listening at 80 port, such that if client is requesting /, then returning the frontend page, if client is requesting /api, then redirect the request to 8000 port of localhost.
My problems are:
Is the above way recommended in the modern web application design? Are there any other ways of hosting both frontend and backend application in the server?
What web server can I use? Apache, Nginx, or manually write a web server?
I use two docker containers to contain the frontend and backend app. Do I need to dockerize the web server as well?

how to remove port number from url in aws while connecting to the API server

I am able to connect to my Nodejs API server through aws by entering url such as ip + :3000. Now I do not want to show port no 3000 after url. is there any way to hide this or this will be there compulsory?
You can't do this on the client side. You need to do one of:
Have your nodejs server running on port 80/443
Run a webserver (apache,nginx) on your nodejs server as a reverse proxy
Use a load balancer.

Resources