PM2 not showing Express app in browser - node.js

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.

Related

Can't find my node js and express project url

I have a node and express project and put it on cpanel. Now I can't access its url.
The path of my project I put in cPanel: public_html > backend.
And the path I want to access from my browser:
https://mydoamin/backend/apiV1/getGender
And I also tried this path:
https://mydomain/apiV1/getGender
Unfortunately, I do not have access to any of them.
NOTE: The server I use is VPS with CentOS7 operating system.
Thank you for your cooperation.
PROBLEM SOLVED
I used pm2 to run the program and now it works fine because the server is running temporarily and it is necessary to use pm2. Because it helps to keep the server running permanently.

Is there a command that allows you to restart a pm2 app if a file in another directory is changed?

I have an Ubuntu server that is running plesk obsidian to manage my websites, on the same server I have a nodejs application running with pm2.
There is a json file that a website and the application needs to access. I am able to make the app's code point to the file and access it, but when the website makes changes to the file it doesn't automatically restart the nodejs app as the file isn't in the same directory.
Is there a command that I can use to make pm2 check for changes with the json file and restart the app?
yes there is a command
pm2 start app.js --watch
here app.js is the file name which cause pm2 to restart

How to really debug node.js app?

I installed node-inspector.
I run it: node-debug app.js
But it opens the empty browser.
If I try to run node.js in debug mode before: node --debug app.js
It kind opens my scripts in browser.
But it's still not clear how to make it work. What to do next? Which url to use to see the working app itself in browser and trigger app to go to breakpoints?
My express app is usually running under localhost:3000. Should I run this url? It's not working.
Thanks
Okay,
I was able to figure it out. The problem was my app is Express based and it's a little bit different. For some reason it's not easy to find these differences until you start digging into every details. There is the instructions how to run debugger if you app is working under Express.
Prerequisites:
Windows 7
node.js app based on Express
Now:
Step 1. Run app in the debug mode (staying in the folder where your app.js located in the first command prompt), do either of 2: node --debug app.js or node --debug bin/www.
Step 2. Open the second command prompt and run node-inspector bin/www (or node-inspector app.js)
If you see the error that it can't access port 8080 it means that something is already listening to it. So you need to run this command differently: node-inspector --web-port=8099 bin/www
After step 1 you will able to run your app as usually in browser: localhost:3000.
After step 2 you open another browser window and enter: http://127.0.0.1:8099/?port=5858

Unable to start the nodeapp through terminal

I have a node app locally and I normally run it using the run button in the web storm, which works fine. While I am documenting the project I came across this blocker: if I try running the same app through the terminal using the command: node app.js , the server starts but the browser throws and error stating "This site can’t be reached" "localhost refused to connect".
Because the port is occupied.
You can change it using app.listen({port})...
or
linux:
export PORT=4500
node server.js
windows:
set PORT=4500
node server.js
I found this myself.. since I used the scaffolding app and it stores info related to server at bin/www, so we will not be able to run the app using the command : node app.js but instead we could run the app using nodemon(which I installed globally on my machine)
Love nodemon for this awesome feature <3 :)

How to start server by my source code (app.js) using FTP?

I'm new to develop server. I already coded my server app.js with Node js and I tested it locally and successfully.
The way I tested my server is that after executing the terminal, moving to the directory having app.js file, command app.js. So then my server is alive and I could test what I want to.
But it's my first time to use FTP to make my server globally. By FTP, I don't know how to do 'node app.js' command.
And if it's done, I don't know the path to test my GET, POST methods.

Resources