Skip to main content

How to Deploy a Node.js Application

Quick Intro

Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, allowing you to build scalable and efficient web applications. With Node.js, you can create everything from simple websites to complex APIs and real-time applications.

Important To Know Before You Begin

Each domain on WebHostMost has a default index.html file located in the document root directory: File Manager > domains/yourdomain.com/public_html. This file is automatically created when the domain is set up and must be manually deleted before deploying a Node.js application. The creation of a Node.js app does not remove this file, and it can interfere with your app if not deleted.

warning

Important: Deleting the Node.js app via the Web Control Panel does not remove the files from the File Manager. You must manually delete these files if they are no longer needed. Additionally, you do not need to configure any ports for your Node.js application; the server automatically handles port assignments.

Setting Up Your Node.js Environment

  1. Access the Node.js App Selector:
    • Navigate to Web Control Panel > Website Management > Node.js App in your Web Control Panel.

Deploy Node.js App

  1. Create a New Node.js Application:

    • Click on Create Application.
    • Fill in the following details:
      • Domain: Select the domain or subdomain where you want to deploy the app.
      • Application Mode: Choose between Development or Production mode.
      • Document Root: Set the directory where your application files are located (e.g., domains/yourdomain.com/public_html).
      • Application Startup File: Typically app.js or index.js.
      • Node.js Version: Choose the version of Node.js that your project requires.
    • Click Create to set up the environment.
  2. Virtual Environment Setup:

Virtual Environment

  • After creating the Node.js application, a link to the virtual environment is provided at the top of the Node.js App page in the Web Control Panel. This virtual environment must be activated before you can install packages or run commands via the terminal.

Uploading Your Project Files

  1. Upload Files:

    • You can upload your project files using the Drag and Drop feature in the File Manager, SCP via Terminal, or Git. Ensure all necessary files are uploaded to the document root directory you specified earlier.
  2. Install Dependencies:

    • Access your server via SSH or use the Terminal in the Web Control Panel.
    • Activate the virtual environment by running the command provided in the Node.js App section of the Web Control Panel.
    • Navigate to your application’s directory:
      cd domains/yourdomain.com/public_html
    • Once the virtual environment is activated, you can install your project’s dependencies:
      npm install
tip

Pro Tip: Make sure your package.json file correctly lists all dependencies and scripts needed to run your application.

Configuring the Application

  1. Environment Variables:

    • Set any required environment variables in your application. You can do this directly in the Web Control Panel under Node.js App > Application Settings or by using a .env file in your project directory.
  2. Application Port:

    • By default, Node.js applications use port 3000. However, on a shared hosting environment, the system assigns an internal port 80. The Node.js selector will manage this for you, so you don't need to worry about configuring the port.
  3. Restarting the Application:

    • After configuring your app, restart it from the Web Control Panel under Node.js App > Manage Applications > Restart.
warning

Important: Ensure your application listens to 127.0.0.1 instead of 0.0.0.0. This prevents unauthorized external access to your app.

Testing Your Application

  1. Access Your Application:

    • Open your browser and navigate to the domain or subdomain where your Node.js app is deployed. Ensure everything works as expected.
  2. Checking Logs:

    • You can view your application logs directly from the Web Control Panel under Node.js App > View Logs. This is useful for troubleshooting any issues that arise.

Best Practices

  • Use Version Control: Ensure your Node.js project is under version control (e.g., Git) for easy deployment and rollback.
  • Optimize for Production: Minimize and bundle your JavaScript files, remove unnecessary dependencies, and use environment variables to manage configuration.
  • Security: Regularly update your dependencies, avoid using outdated Node.js versions, and follow best practices for securing your app.

Troubleshooting Common Issues

  • Application Not Starting: Check the startup file and ensure it’s correctly defined in the Web Control Panel. Also, check for errors in your code or missing dependencies.
  • Port Issues: Verify that the application is configured to use the port assigned by the Node.js selector.
  • Slow Performance: Ensure you are in Production mode and that your server resources are sufficient for the application's load.

Conclusion

Deploying a Node.js project on WebHostMost is seamless and efficient using the tools provided in the Web Control Panel. By following the steps outlined above, you can have your Node.js application up and running in no time. For further assistance, don’t hesitate to contact our support team.


Deploy your Node.js apps with confidence using WebHostMost's powerful and flexible Web Control Panel.