Running Mongo Node Angular on same server - node.js

Can I / Should I host Angular, Node-express and Mongo on same server, say localhost:3000 or somehosting.com/server-address?
Also is it a good practice?
I've seen Angular and Node running on same server, but what about adding mongo too?
like everything equivalent to
mongodb://localhost:3000/db
ng serve --port 3000
server listen(3000)
Thanks

As you have not mentioned for what purpose - the answer will be depending on the purpose and context. If you are learning, then running webserver, database and serving Angular static files , all from the same server is not an issue. However, if you are going to have live web app, even with less or moderate traffic, then you must run database on its own server, and the webserver and static Angular files from one server. Of course, for SPAs that expect lot of traffic and real use, it is better to serve your static Angular files from a CDN or storage service like AWS S3, and web and database servers separately.
Happy learning!

It is best practice to keep the databases(stateful) in different servers then the applications(stateless) unless it is just for testing...

Related

How can I access Heroku NodeJS server externally?

I have a websocket server running via NodeJS, and have deployed it to Heroku. There are two separate web applications I wrote that communicate with the websocket server. On localhost, I simply run the node server, load up the applications in a web browser, and all works fine. In Heroku, however, I can't seem to get anything to connect to the url ws://url:port - it just returns an err request timeout.
I don't want to deploy my html using Express per their example. Maybe I could, but this is a test case where some web applications get to be pretty huge.
Is my issue that I have to use wss instead of ws?
Or backing up further, is this a good use case for Heroku or is there something else I should use?
The easiest description of what I'm trying to do: Two websites hosted somewhere that isn't Heroku both communicating with a NodeJS-based websocket server hosted on Heroku.
Thanks for your help!
Alright, I feel pretty dumb because this is about as easy an answer as it gets. For anyone else trying to do the same thing, you don't need to specify the port (Heroku port is for internal use only, and the heroku url you are provided is on port 80). Simply connect using wss (e.g. wss://your-app.herokuapp.com)

How to use Nginx to load pages through express router

So I'm building an end to end application (With node.js/mysql back end, react front end, and using the express router), but I'm having trouble setting up a local development server. I don't need it to be accessed from the outside world, just be able to load different pages connecting to the express router. I don't have any dev ops experience for this, so I'm trying to use nginx to point it to the router which I can't figure out. Is there an easier way to do this?
I also need to run this on a windows machine, which just makes everything slightly more complicated
It's not entirely clear from your description how your application is set up and what the role of Nginx is.
So I'll start from the beginning...
Nginx is primarily an HTTP server which can also function as a proxy for HTTP requests. If you've written a Node.js application using Express, you have written an HTTP server which can handle any routes you have set up and can also serve your static assets (ie. HTML pages, images, front-end Javascript, CSS, etc.). In this case, there is no need for Nginx - if you wrote something like the Express "Hello World" app, then you will see a message like "Example app listening on port 3000" and you can connect to your app by visiting http://localhost:3000 in your browser.
That's it - there's literally nothing else to your app and there is no need for Nginx (or any other HTTP server) to run your application.
Now that's not to say that there is no role for Nginx in your application, but it may not be as an HTTP server. One possibility is that you may want to set up Nginx as a proxy, to handle certain routes by sending the requests to your Node application. For example, I set up an application some time ago which uses Nginx to proxy API routes for my application to a Node application and to serve static assets directly. This may be what you have in mind - if it is, you will need to configure different routes in Nginx to serve different things (and unfortunately there's not enough information in your question to give suggestions on this).
As an aside, you're probably going to find this much easier to set up using Linux - perhaps the Windows Linux Subsystem, a virtual machine running Linux, or Docker.
You'll probably want to use
https://github.com/facebook/create-react-app
create-react-app my-app will set up everything you need (webpack, etc.), and then
npm start will start a local development server.
Should work on Windows, but I don't know, because I wouldn't use/recommend Windows ;-)

Hosting a webapp with a ReactJs frontend and ExpressJs backend

The frontend is a React SPA and the backend is NodeJs app that exposes an API. The frontend queries the API for data from time to time but other than that it is fairly independent. What is the best way to host an app like this? Should I include the build folder in the NodeJs app and have the express server serve the static contents from a route? Or should I host both separately, set up a Nginx server for the React app on something like DO? I will host the backend on something like Heroku or Google App Engine. So considering this, what is the ideal solution? What are the pros and cons of either approach?
In case of production, include build folder in the nodejs app. Performance increase in case production. You can refer react docs for details.
In case of development, host it separately, so its easier to work on it.

Serve angular in node vs nginx

just a quick question.
What would be more beneficial, serving my angular application via node with a reverse proxy from nginx or just serving it directly from nginx?
I would think it would be faster to serve it direcly from nginx.
If there is a clean separation of your client-side code and your server side code (e.g so anything the client needs to run is either pre-built into static files or served using your rest api), then it's far better to serve the client-side files either directly from NGINX or from a CDN. Performance and scaling are better, and there is less work for you to do in code on the server to manage caching, etc. plus you can later scale the api independently.
nginx(as a reverse proxy) + nodejs - It's the best choice.
You will have much more benefits if you choose nginx as a frontend for nodejs. (ssl, http2, configuration, load balancing etc.)
If we think about static files (js, html, images) - it's more easier to cache them in one place (nginx host config) node also works with static file quite good.
I think that nodejs engine/server should do only one thing and it's business logic of the application.
Depending on your load requirements. You can setup multiple instances(runtimes) using nginx+node. If you have high load js application, i would suggest going for this solution. Otherwise, this does not matter.

How to implement create-react-app with node.js backend with heroku?

I am looking to serve out some data from some json and csv files to my front end, which is built on create-react-app.
My idea is to have them both listen at different ports (3000 for React, 3001 for express backend) and then make API calls to 3001 to get whatever data I need through ajax.
How can this work in production?
For instance with Heroku, how would I go about deploying this since it's listening at 2 different ports?
What unforeseen issues will this have/ is there a better method?
To add some more info:
All my backend is doing is parsing some csv and json and serving it out in formatted and edited json.
If you use Heroku, it is recommended to use two dynos separately. One for serving static files and react, and the other for API server.
Or you can use PM2 to do the same things in one dyno, by using fork mode of it.
In both case, as two servers don't share the same port, you will have some problems using sessions, and troubles to make API requests. I think it can be solved by using token based authorization like jwt or using separate session storage like redis

Resources