DEV Community

Cover image for How to create a Node.js web server with cPanel
Brian W.
Brian W.

Posted on

How to create a Node.js web server with cPanel

Welcome to this article, where I guide you on creating your own Node.js Express web server with cPanel. This guide will apply for most domain registrars which offer hosting services with cPanel, such as Namecheap or Hostgator, but for this tutorial, we will be using Spaceship Shared Hosting.

Spaceship is a domain registrar owned and operated by Namecheap which offers competitive domain pricing for most top TLDs. They offer a variety of different services for websites, such as website hosting and emails.

Without further ado, let's get started!

Before we continue, make sure you have a domain that is already added to your cPanel. If you already have this setup, go ahead and navigate to Software tab. Click the Setup Node.js App to start the process.

Software tab in cPanel

Once you're on the Node.js page, click Create Application and fill out the following details:

  • Node.js Version: Select a desired Node.js version for your web server. It's highly recommended that you select a higher Node.js version to ensure that Node.js is supported by npm and isn't outdated.
  • Application Mode: Select the option applicable in your case. If you are unsure of which option to select, select the Production option.
  • Application Root: This is the folder name that will contain all of the files for your Node.js App. Make sure the name isn’t the same as your domain, because all of the files will be served (yes, your code could be leaked!).
  • Application URL: In the dropdown, select the domain that will be used for your Node.js App.
  • Application Startup File: Enter the name of the main Javascript file, which will run whenever you start the Node.js App. For this tutorial, we will use index.js. (if you'd like to add any environment variables, you can add them now)

Node.js Configuration Example

Once you fill out the details above, click the Create button to finish the process (be patient!).

After your Node.js App has been created, go back to your cPanel and locate the Files tab. Then, click the File Manager option to launch the file manager.

Files tab in cPanel

Once you're in the file manager, click the name of the folder that we created earlier for our Node.js App. When you open the folder, you may already see an index.js file located inside. Click the file, and then click Edit from the top bar to open the file.

In the index.js file, let's create some code for our Express server. Remove the old file contents, and enter the code below:

const express = require('express');
const http = require('http');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, world!');
});


const server = http.createServer(app);
server.listen();
Enter fullscreen mode Exit fullscreen mode

Click the Save Changes button on the top-right of the screen, and go back to the file manager tab.

Next, we'll need to create a package.json file which will contain our dependencies. This is crucial for our Node.js App to run properly, and will allow us to install npm later on.

Click the + File option located on the left side of the top bar, and name the file package.json. Then, click Create New File to confirm your changes.

Open up the package.json file in the editor as we did previously, and insert the following JSON code:

{
  "name": "example-express-server",
  "version": "1.0.0",
  "description": "A simple Node.js Express web server.",
  "main": "index.js",
  "dependencies": {
    "express": "*"
  }
}
Enter fullscreen mode Exit fullscreen mode

Click the Save Changes button to save the contents, and go back to the Node.js dashboard in cPanel.

Once you're back in the Node.js App dashboard, click the pencil icon (Edit your application) to open up your app. Scroll down and click the Run NPM Install button to install the dependencies in our package.json file.
Configuration file tab in cPanel Node.js

In rare cases, you may receive an error like this:
An error occured during installation of modules. The operation was performed, but check availability of application has failed. Web application responds, but its return code "None" or content type before operation "text/html" doesn't equal to contet type after operation "text/html; charset=utf-8".
If you receive this error, don't worry! The process has still been completed successfully, and you may continue with the guide.

Once the process has finished, click the Restart button to start your Node.js App with the new updates. When your Node.js App restarts, open a new tab and visit your domain.

Privacy error on Chrome

Oh no! When visiting your website, your browser may warn you that the connection is unsafe. In order to solve this issue, we'll need to generate valid SSL certificates! Don't worry, it isn't as complicated as it sounds.

Visit the cPanel dashboard, and navigate to the Security tab. Click the SSL/TLS Status option, and then locate your domain in the list provided.

Security tab in cPanel

Once you find your domain, click the + Include During AutoSSL option. Make sure to do this on the www subdomain of your site as well. Lastly, click the Run AutoSSL option to create an SSL certificate with Let's Encrypt. This process may take a few minutes, so be patient!

AutoSSL in cPanel

Once the SSL certificate process is completed, your cPanel page will refresh automatically. You will then see a green lock next to your domain name, with the label AutoSSL Domain Validated.

Lastly, go back to your website and refresh the page. The page will automatically redirect to HTTPS, and you will now see that your website is secure.

Congratulations! In this guide, you learned how to set up your own Node.js web server with Express using cPanel, and you set up SSL certificates to secure your website. If you found this guide useful, or if you have any questions, let me know in the comments below. Bye for now! πŸ‘‹

Top comments (0)