how to deploy expressjs application on window server - node.js

There many tools to deploy expressjs application, such as strongloop,pm2.
But they do not work well on windows.
I have install [pm2]2 in my window computer, and start my app.
https://i.stack.imgur.com/ZpOIa.jpg
But when i log off my computer and log in again. the service is disappear.
So I install pm2-windows-service and install pm2 as a window service. It failed again.
Anyone have a good expressjs deployment plan?

You need to make a pm2 save when you started your app. PM2 need to know which apps were started and their state to restart them after a reboot, and thats the aim of the pm2 save command which will save the state of all apps in the pm2 directory.

Related

Error running nextjs with PM2 on Azure App Service on Linux

The pipelines and releases seem to work finely as they get the artifact deployed on the app service or so it seems as the output error happens on start of the PM2 process.
I haven't found anything on this error on google, and don't know if there is a way to update PM2 on the app service machine as PM2 itself suggests.
These are the logs.
Error message
summary image
I was expecting the app to work and no longer show an application error as I fixed both build and release pipelines.
This is a picture of the important bit of info of the error as its really long
Check the below steps to run nextjs with PM2.
First create a nextJS app.
Run npm run dev command to run the app in development.
Deploy the app to Azure Linux App Service.
Open the SSH of the Azure App Service to run the PM2 commands.
We can open directly with the below URL.
Path to SSH - https://YourDeployedAppName.scm.azurewebsites.net/
OR
Navigate to the deployed Azure Linux App => Advanced Tools => Go => SSH.
Run the below command to install PM2.
npm install pm2 -g
Thanks #Azure OSS Developer Support for the commands.
In Configuration section => General Settings add the Startup command.
pm2 start site/root/index.js --name mynpmnextapp
Your path may differ for the index.js file.
My app entry point is index.js.For some apps it can be server.js.Change it according to your app.
cannot find module `../build/output/log'
Make sure you are not running the PM2 with the output folder. As mentioned above it has to be the entry point either server.js or index.js based on your code files.
don't know if there is a way to update PM2 on the app service
In the KUDU Console use npm install pm2#latest -g to update the PM2.
References taken from MSDoc and npmjs.

Node app crashes after closing the terminal, even using pm2

I'm doing web automation by using Puppeteer with NodeJS. It works fine when I'm logged in to the terminal. I'm using ec2 instance Ubuntu 22 server.
But when I'm exit out of the terminal and then try to perform the task it just loads and loads and do nothing. But when I log in to check the pm2 logs then it starts working again with the terminal open.
When I check pm2 logs I get this error every time.
Error in pm2 logs:
I suppose there is problem with the command you are trying to run the app. Use the following command to run the app via pm2.
npx pm2 start app
For Cpanel Users having this issue
Login to your WHM account, click on the terminal, cd into the directory where your node js application is, then pm2 start .This was how i solved my issue.

What is the best way to restart a node app automatically on 1) server restart AND 2) changes made to the files?

Apparently:
nodemon helps with changes to the files (e.g. you make changes to index.js)
pm2 and forever keep the app running if you close the terminal, and can restart the app if it crashes.
But: What is the best combination or tool (and how to use it) to
Start the node app automatically on system boot/reboot
Restart the app automatically when changes are done to the files?

How to deploy a NodeJS app without root access, so it stays online after server restarts?

I'm trying to deploy my NodeJS app. I tried npm forever and pm2, but I believe that my hoster restarts the server every night because my app is always offline the next day.
The thing is, that I have a shared hosting server on A2hosting and don't have root access, so these sudo commands don't work here... except you know how I can change that, haha...
I don't know what infos I need to provide for you to help me... so if you need to know something, tell me please!
Thank you so much!
I've used pm2 to do that.
npm install -g pm2
then you run the following command to ensure pm2 will run on startup:
pm2 startup
It will show you the command you have to run.
Finally, you initiate the process:
pm2 start server.js --name <service-name>
Freeze your process list:
pm2 save
More details on pm2 documentation: https://github.com/Unitech/pm2

pm2 difference between stop and delete app

In pm2 node app manager, what is the difference between stop and delete app. I know that delete app deletes the app, from the pm2:s control, but what does stop app do? They both will set node server to offline.
My problem is that during deployment, if I want to pull code, and then restart the node server, then which pm2 commands to use? What I have done now is first pm2 stop app -> pull code -> pm2 start app. But how do I know that the app.js is really updated? What if stop puts the app in memory, and loads it there? So after start, it will start the previous version, and not from the code that was pulled.
Stop command keeps the app in the Apps list, delete command not.
You can see the Apps list with the command:
pm2 status
So if you stopped, you can restart your app just by its name.
I think the command you want is:
pm2 reload [AppName]
Just replace the files and then run the command.
Source:
http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
You can handle the reload signal inside you app, what could be interesting in production. More info:
http://pm2.keymetrics.io/docs/usage/signals-clean-restart/

Resources