User Tools

Site Tools


nodejs:production-mode

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
nodejs:production-mode [2024/04/04 05:37] – created kirtisinghnodejs:production-mode [2026/03/19 10:00] (current) kirtisingh
Line 1: Line 1:
 ===How to run/work with production mode for NodeJS Apps=== ===How to run/work with production mode for NodeJS Apps===
  
-**Step 1 :** Log into N99panel. From under N99panel dashboard, click 'Node Apps'+**Important - Before running your app in production mode, kindly test your app in development mode first. Only after extensive testing, you may then use the production mode.** 
 + 
 +**Step 1 :** Log into N99panel. From under N99panel dashboard, click 'Node.js Apps' 
 + 
 +{{:nodejs:nj2n-app-prodmode.png?direct&400|}}
  
-{{:nodejs:click-node-apps.png?direct&400|}} 
 \\ \\
  
-**Step 2 :** Now click 'Link a Node App+**Step 2 :** Now click 'Add a Node.js App
 + 
 +{{:nodejs:nj2n-click-app-prod-app.png?direct&400|}}
  
-{{:nodejs:link-node-app.png?direct&400|}} 
 \\ \\
  
 **Step 3 :** Fill in the form as under **Step 3 :** Fill in the form as under
  
-{{:nodejs:dev-form-prod.png?direct&400|}}+{{:nodejs:nj2-nodeappprod-form.png?direct&400|}}
  
-**Important point :**\\+\\ 
 + 
 +**Important points :**\\
   - Select the User/VHost (website)\\    - Select the User/VHost (website)\\ 
   - Enter the ‘App Web path’ . This would be like /app1 or /app2 or it can even be the website root i.e. /\\   - Enter the ‘App Web path’ . This would be like /app1 or /app2 or it can even be the website root i.e. /\\
-  - Enter the ‘App Port’ . The port entered under should be within the range of 40000 to 50000\\ +  - Enter the ‘App Port’ . The port on which your App runs . You need to ensure that your App listens on interface 0.0.0.0 instead of 127.0.0.1 or localhost which typically are used\\ 
-  - Select the ‘App Mode’. This should be ‘Production’ variant here in this example we have choose 'Production (node)i.e. the app is fired with node command \\ +  - Select the ‘App Mode/Initiation’. It will be one of  
-  - Under the production filesystem path, enter the folder containing the app w.r.t. /home/{username}/node_apps/ . So in our case it is app1+    - 'node' command 
 +    - 'npm start' command 
 +    - 'npm run dev' command 
 +    - A command after 'npm'  
 +  - Under the Production filesystem path, enter the folder containing the app w.r.t. /home/{username}/node_apps/ . So in our case it is app1 with App mode/initiation set to 'npm start' . But if our app had to be initiated via node command, then production filesystem path will be app1-project/index.js
   - Also finally select the Nodejs version that you wish to use with the App   - Also finally select the Nodejs version that you wish to use with the App
 +  - If you app use .env file(s), please provide its full path under 'Env Files' box. You may enter multiple .env files, one on each line
 +  - Also if you wish, you can expose other directories of the user to your app. Lets say you are storing PDF files in /home/bluser/pdf-files , then in the box 'Other Directories' box you need to enter pdf-files . Multiple folders/directories can be exposed, one on each line.
 +
 +In production mode the Node app runs w.r.t. system path reference. **To put it simply you need to ensure that any file references in your code, are absolute and not relative.** Lets say your Node app start point is /home/bluser/node_apps/app1-project/index.js , and your index.js refers to contents.json , you would need to change it to /home/bluser/node_apps/app1-project/contents.json .  
  
 With the above steps, you have mapped the web-server's VHost/Website’s URL part to the to the specified port of the App, which will be launched by our environment from the specified source folder With the above steps, you have mapped the web-server's VHost/Website’s URL part to the to the specified port of the App, which will be launched by our environment from the specified source folder
  
-Read further on how to start the App when logged into SSH +**If your App is not working as expectedplease visit the following article in order to debug it in production mode : [[:nodejs:production-mode-debug|How to debug NodeJS apps in production mode]]**\\
- +
--------------------------------------------------------------- +
- +
-N99panel comes with multiple NodeJS versions i.e. 1012, 14, 16, 18, 20. These versions can be referenced from within SSH using the commands  +
-<code> +
-node10 +
-node12 +
-node14 +
-node16 +
-node18 +
-node20 +
-</code>  +
-respectively. The npm’s accompanying these versions can be referenced via  +
-<code> +
-npm10 +
-npm12 +
-npm14 +
-npm16 +
-npm18 +
-npm20 +
-</code> +
-respectively. The default node command refers to node18. Please refer to this article to change the default version of node CLI - [[:nodejs:cli-version|How to change the default version of node or NodeJS CLI for a user]]\\ +
- +
- +
-Now to run the App, +
- +
-Login into the server/VPS via SSH of the user ID under which the VHost/website is running. +
-Though you can run the NodeJS App from any folder, but ideally create a new App folder within the node_apps folder. This would help later also when we shift this App to the Production mode. +
- +
-Now assuming that you created a app folder app1 , now under that folder upload all your source files including package.json . Lets’s say that we wish to user version 16 , to run NPM commands w.r.t. that ideally we will use npm16  . +
- +
-To run the app, we should first ensure that the NodeJS source file creates the Node server at the ‘App Port’ number as specified earlier. We’ll explain further via example +
-Let’s say the user port range is 50051 to 50060 , and the port we entered under ‘App Port’ is 50051 +
-Now my app file is say app.js . In my app.js I would need to create the server on port 50051 . +
-To now actually run the app, we just need to type +
- +
-<code> +
-node16 app.js +
-</code>   +
- +
-Above process would now start the App as a foreground process +
- +
-To start the App as a background process, do the following +
- +
-<code> +
-node16 app.js & +
-</code>   +
- +
-If there are errors, it will simply show the errors and the process would end. And when you fix the errors, you can again try starting the App. +
- +
-When you believe that you app is running stably and as per your expectations, at that moment you can decide to shift your App to the Production mode. Click here to read about it.+
  
 +To manage the App process, you can do the same from within Dashboard » Node.js (apps mode) » Manage App State (apps mode)
nodejs/production-mode.1712209040.txt.gz · Last modified: 2024/04/04 05:37 by kirtisingh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki