How to deploy different MERN apps to digital ocean on a single droplet? - node.js

I've always used heroku to deploy my MERN apps. For the mongo db I use MongoDB Atlas, but in my job they want to migrate all the projects to DigitalOcean. I have several questions regarding this:
Can I have mongoDB + nodejs backend + react app on a single
droplet?
Can I deploy two or more apps in a single droplet? (The
apps have different domains)
Is there a video tutorial about this
(I've read lots of documentations and got many errors while trying
to do it. My eyes hurt 🙃)
For example if I have in Heroku two apps for the same website, one app for the nodejs backend and another one for the react frontend... can I do the same on DigitalOcean?
Thanks in advance!

Yeah, you can deploy multiple services in a single server, they just need to be listening on different ports.
For example, let's consider that a MongoDB server is running on port 27017, a Node.js http server is running on port 5000, and a React app is running on port 8000.
Say, your server's IP is 13.13.13.13.
Then you can access your MongoDB server, Node.js http server, and React app using 13.13.13.13:27017, 13.13.13.13:5000, and 13.13.13.13:8000, respectively, from anywhere in the Internet where your IP isn't blocked.
Now, in your server, you set up iptables to forward all incoming connections from port 8000 to 80. Now, you can access your React app by visiting 13.13.13.13, no need to use the port anymore.
Now, let's say, you add DNS records for example.com and api.example.com to point to your IP. And since you can't have A records or CNAME records pointing to a port, both of your domains will direct you to your React app. You'll have to explicitly specify the port number along with your domain if you want to access your Node.js backend, like http://example.com:5000, or http://api.example.com:5000.
If you don't want to access your backend using the port number, you can make use of Nginx as a reverse proxy. You can set up Nginx to route all the traffic to api.example.com to your backend server listening on localhost:5000.

Related

Deploying back-end and front-end on AWS

I have a full-stack application with Node and express for the back-end (with Postgres in a AWS RDS created already) and Angular for the front-end.
Now, when running locally in development I have the back-end listening to port 3000 and conected a Pool client with a Postgres DB in AWS RDS. Separately, I have my front-end listenting to port 4200.
When running the server and the angular in these two different ports and opening my browser everything works fine.
Now my questions are about how to deploy this same structure in AWS all together.
Shall I deploy in AWS the back-end and front-end listening to these two different ports (as deployment) or they should listen to the same one and add a proxy server like Ngnix like I have been reading?
In the last case, how?
If you have a simple setup, and you always deploy frontend and backend at the same time, one option would be: just let you express backend also serve your static Angular files.
This avoids any unnecessary complexity and components.
So, you have both frontend and backend exposed at the same port. Maybe all your backend APIs are listening under the /api route. Everything else is served statically, with a fallback to index.html for client-side routing.
By having frontend and backend on the same origin (host:port), you also avoid any CORS issues.

Best practice for Deploy Node Express application and php application on AWS ec2 instance

I have an AWS ec2 instance along with one domain. I want to deploy my express node application and PHP application on the same server without adding a port with the domain.
For PHP deployment I have configured xampp server with a default port of 80.
And my node application runs on port 3000.
Currently, I am accessing the node app using - mydomain.com:3000
and for the PHP - mydomain.com/website
I wanted to use an express node app without entering the port number. example - mydomain.com
Is there any way to configure it with my given scenario?
Perhaps what you want is to run two different applications on one physical server.
How about approaching the concept of load balancing on one server?
Typically, you can implement this using nginx's reverse proxy and upstream.
For example, if the node server is running on port 3000 and the php server is running on port 4000
In nginx node.mydomain.com is port 3000
php.mydomain.com can send on port 4000.

How to remove port from url for node application using nginx

My react and angular application (UI have two parts) are running using
node/express application on port 3000. On server.js (node-express entry point) I dynamically
handle which UI to render (react or angular at a time) on a browser using
express-static feature.
Earlier my application is running on - https://mywebsite.com:3000/ but
as per requirement it should be changed to - https://mywebsite.com which we
handled using "nginx proxy" with DevOps person but now encountered
another issue actually now UI is accessible by using both URL that is
https://mywebsite.com:3000/ and https://mywebsite.com. I want it should be
accessible by using https://mywebsite.com/ only without port.
My server's API's (https://mywebsite.com:3000/api/v1 ) is accessible from three places: -
1) iOS app
2) Admin app (running differently)
3) and UI ( React.js + Angular.js) (https://mywebsite.com)
Note- Is there any way to handle this either through the deployment process or setting at node/express server level. We found one solution that is to create a separate node server for the UI part but as per cost-cutting, we ignored this approach that is creating another server for UI.
Suppose my domain name is https://api.aegisapi.com:3000, first, you can check in your inbound, means HTTPS is present or not if not present then add HTTPS for 443,
then you can run https://api.aegisapi.com. It works.
When you run your node server like this
it listens on all interfaces i.e. 0.0.0.0 which is accessible from outside.
You should your listen to
from
app.listen(3000)
to
app.listen(3000, '127.0.0.1');
Also, you should also block this port with IPTables as well.
if using AWS EC2 instance then open inbound port 3000 and open public facing port e.g. 80 or 443 to get request from users, then as you are using nginx as reverse proxy use that to forward request to port 3000.

Running NodeJS/Express projects on production server

1 With different apps different website domain etc, NodeJS cannot go to production with host:*some port other than 80*, right? If I am wrong, how to deal with NodeJS apps with multiple website on the same machine? ( there is no virtualhost in NodeJS/Express server, isn't there?)
2 So the solution to go prod to me, only alternative is to use some proxy forwarding requests to the NodeJS/Express server IP:port, isn't it? If yes and if it is a different server ( proxy and NodeJS), what does express to start and listen to? (Say, server.listen('port', '0.0.0.0') or server.listen('port', '::')?
3 Any other alternatives to go production with NodeJS/Express projects?
You can use 80 but with sudo. However, it's not recommended.
You're right you need a proxy (nginx, haproxy, etc..) to sit in front of your Node.js app in order to use port 80.
I think you can omit host from server.listen so it will accept connection from ::.
NGINX is the best option to do, what you expect and see the NGINX documentation in official web site.

multiple nodejs apps for multiple domains on the same machine/ip. switching ports

We have multiple domains each have embedded nodejs app we need to make every nodejs app running url like : http://domain_name:port_number
but we have a strange problem that every port we set for any nodejs app could be accessed from one domain not the domain we set the port in its nodejs app except 8080
How to make all ports accessible from all domains running on the same machine/ip or open port number for a domain
we use apache as the web server not using express or nginx
The problem solved there was a dns settings on cloudeflare prevents completing requests

Resources