I am trying to serve my node.js and react application with pm2 including loading data from mongoDB.
So far I can serve the build with serve -s build -l 3001 i am using port 3001 for the front end.
And pm2 start server.js for the backend which runs on port 3000. This runs and loads data successfully.
However, instead of using pm2 start server.js I want to be able to run pm2 serve build/ 3000 with my app name however when I do this my web page turns blank, all responses are 200 and there is nothing in the logs.
I have also checked online and searched many resources all result back to exactly what i am currently doing.
I am using npm run build to create my build folder.
Does anyone know a way to successfully serve your react build directory with pm2 ?
Thank you!
Related
I'm trying to create an app in Angular 7, following the tutorial that gives you the default page
ng new my-app
I already reviewed it with ng serve --o and it works, but I have no idea now how to pass it to an environment outside of Angular. I've done
ng --build --aot = true
This has created a folder / dist / my-app and 3 JS files come in: main.js, polyfills.js and runtime.js I opened the index.html but nothing is shown, I expected to see the same thing I was doing in development.
I tried to run them with node, doing for example node main.js but it has not worked, in the 3 options it says
window is not defined
Has anyone already done these deploys to see how it would be in production?
What am I doing wrong....
Apps built with Angular or similar frameworks such as React or VueJs needs to be deployed in a weserver. Simply opening the index.html file in the browser will not work.
Now there are several ways of doing this -
Development
You can use http-server
npm install http-server -g
http-server -p 8080 ./dist
You can visit http://localhost:8080 to view your server
Production
There can be several options for production deployment depending upon a variety of factors such as your cloud provider, web server etc. The following two are an example -
Firebase Hosting
Firebase has a very simple deployment step - do firebase init once and firebase deploy to deploy your newly built webapp.
You can use Nginx running in docker
Dockerfile:
FROM nginx:alpine
COPY config/nginx.conf /etc/nginx/nginx.conf
COPY build/ /var/www
what you must do is mount your dist folder, which is the result of the ng-build you made on any http server.
apache: copies the dist folder in the www/html path of your apache
I am trying to deploy an application I am building with the MEAN stack to Heroku and am having some trouble. I think the issue is with the structure of my application. I have all my server code in a folder called server, which has its own package.json and src folder that contains the actual server code.
Right now I am simply trying to get Heroku to deploy the client side of the application. I am only getting an error... I know that the database and server are not running but I cannot even get past the initial displaying of the app. I have one web dyno set up to run ng serve (npm start) on the app.
If someone would be willing to look at the structure of my application and sees why I am unable to deploy to Heroku without really digging into the code, that would be much appreciated.
Here is the code
Please note that it is on the deploy branch, and this is on purpose. I do not want to push anything to the master until I am sure it is working.
The Angular web server targets localhost:4200 by default. That can be changed with a couple command-line options. --port accepts a port and --host accepts a host IP address.
So you could modify the ng serve script as follows: ng serve --host [host-ip] --port [port-number] --disableHostCheck. That last flag (--disableHostCheck) tells the dev server to bypass host checking when normally ng serve fails on anything except localhost. Terrible idea if meant for anything except private development/testing.
Another issue: Heroku runs off web dynos and from what I understand about them, they use a random port and IP each time. While the random port is stored under the variable $PORT, the IP does not seem to have a similar variable. Web dynos keep that information to themselves.
Heroku does offer this command: heroku local web. It runs your application on localhost:5000. That means ng serve --port 5000 works perfectly with this command. This should tell you how your front-end will run on Heroku. Your angular dev server will function as expected too.
For actual deployment to Heroku, use that express server of yours. Run ng build from your file system and it will spit out an index.html in the dist folder. This file holds your entire front-end application. You can then upload that file into your browser from the server.
For express that usually breaks down into:
app.get('*', function(req, res) {
res.sendFile('path/to/index.html');
});
Hopefully this helps! Let me know if I missed the mark anywhere.
I have node application hosted in amazon ec2.
I used nginx to point it in my domain.
It runs perfectly when i use npm start to run the application.
But when i use pm2, it doesnt work.
pm2 list command shows that the app is running.
But url doesn't work. Even when i again run npm start app gets started that clearly states that the port is free. So pm2 is not actually running anything on that port. But pm2 says app is online.
I was running the wrong file from pm2. As it was express project, so i had to run bin/www
So the following command solve the issue:
pm2 start bin/www
Answered it here as well:
https://github.com/Unitech/pm2/issues/3252
Thanks
I am using PM2 in my Express app for process management and load balancing. I have just installed the PM2 and given the command pm2 start bin/www
this command works in command prompt, it is showing the result like this:
This is fine now I am opening the web browser and accessing the application as usual localhost:3000 but it is not opening the application its is showing ok in the browser.
What could be the possible reason? Please help. Thanks.
I got the solution. We know when we create an app using Express (Express generator) it creates all the folder and places app.js file in the root folder. For some reason, I kept the app.js file inside a folder and changed its path in bin/www file. It was working perfectly when I was running npm start but with PM2 it was now opening the application in the browser. So I again placed the app.js file in the root folder and now it is working fine.
I was getting tired of using FileZilla every time I wanted to push a change to my server hosting my website and so I set up a github repo and linked it to my server so that changes and pushed right to the server.
However, my backend is written in node and so each time I update my server.js file I have to restart the server. With "node server.js"
Is there a way to watch the file and programmatically restart the node server when an update is detected?
If it helps, my serve is Ubuntu Linux running apache2
You could try writing something yourself, or use one of the popular libraries that are already out there:
https://github.com/petruisfan/node-supervisor supervisor server.js
https://github.com/remy/nodemon nodemon server.js